multi-store/app/ExceptionHandler.php

43 lines
1.4 KiB
PHP
Raw Normal View History

2024-05-30 21:37:55 +08:00
<?php
2024-06-03 17:28:23 +08:00
namespace app;
2024-05-30 21:37:55 +08:00
2024-06-03 11:03:08 +08:00
use hg\apidoc\exception\ErrorException;
2024-05-30 21:37:55 +08:00
use Next\VarDumper\Dumper;
use Next\VarDumper\DumperHandler;
2024-06-04 16:51:26 +08:00
use support\exception\BusinessException;
2024-05-30 21:37:55 +08:00
use support\exception\Handler;
use Throwable;
use Webman\Http\Request;
use Webman\Http\Response;
class ExceptionHandler extends Handler
{
use DumperHandler;
public function render(Request $request, Throwable $exception): Response
{
if ($exception instanceof Dumper) {
return \response(self::convertToHtml($exception));
2024-06-03 11:03:08 +08:00
} elseif ($exception instanceof ErrorException) {
return response($exception->getMessage(), 401);
2024-06-04 16:51:26 +08:00
} elseif ($exception instanceof BusinessException) {
return response($exception->getMessage());
2024-06-03 16:14:59 +08:00
} elseif ($exception instanceof \Exception) {
$isDebug = config('app.debug');
$error = [
2024-06-03 17:28:23 +08:00
'code' => $isDebug ? $exception->getCode() : 0,
2024-06-03 16:14:59 +08:00
'msg' => $isDebug ? $exception->getMessage() : '服务器内部错误',
];
if ($isDebug) {
$error['file'] = $exception->getFile();
$error['line'] = $exception->getLine();
}
return response(json_encode($error, JSON_UNESCAPED_UNICODE));
2024-05-30 21:37:55 +08:00
}
// 非json请求则返回一个页面
return new Response(200, [], $exception->getMessage());
}
}