diff --git a/app/Exception/BaseException.php b/app/Exception/BaseException.php new file mode 100644 index 0000000..f526839 --- /dev/null +++ b/app/Exception/BaseException.php @@ -0,0 +1,16 @@ + $throwable->getCode(), + 'message' => $throwable->getMessage(), + ], JSON_UNESCAPED_UNICODE); + + // 阻止异常冒泡 + $this->stopPropagation(); + + return $response->withAddedHeader('content-type', 'application/json; charset=utf-8')->withStatus(200)->withBody(new SwooleStream($data)); + } + + public function isValid(Throwable $throwable): bool + { + return $throwable instanceof BaseException; + } +} diff --git a/app/Exception/Handler/JwtAuthExceptionHandler.php b/app/Exception/Handler/JwtAuthExceptionHandler.php index 59275c8..a3cfa64 100644 --- a/app/Exception/Handler/JwtAuthExceptionHandler.php +++ b/app/Exception/Handler/JwtAuthExceptionHandler.php @@ -21,7 +21,6 @@ class JwtAuthExceptionHandler extends ExceptionHandler $data = json_encode([ 'code' => $throwable->getCode(), 'message' => $throwable->getMessage(), - 'data' => [] ], JSON_UNESCAPED_UNICODE); // 阻止异常冒泡 diff --git a/app/Exception/Handler/ValidateExceptionHandler.php b/app/Exception/Handler/ValidateExceptionHandler.php index cfed111..7083a5d 100644 --- a/app/Exception/Handler/ValidateExceptionHandler.php +++ b/app/Exception/Handler/ValidateExceptionHandler.php @@ -27,7 +27,6 @@ class ValidateExceptionHandler extends ExceptionHandler $data = json_encode([ 'code' => $throwable->getCode(), 'message' => $throwable->getMessage(), - 'data' => [] ], JSON_UNESCAPED_UNICODE); // 阻止异常冒泡 diff --git a/config/autoload/exceptions.php b/config/autoload/exceptions.php index 5fc4b04..4e55998 100644 --- a/config/autoload/exceptions.php +++ b/config/autoload/exceptions.php @@ -15,9 +15,10 @@ return [ Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class, App\Exception\Handler\JwtAuthExceptionHandler::class, App\Exception\Handler\ValidateExceptionHandler::class, + App\Exception\Handler\BaseExceptionHandler::class, App\Exception\Handler\AppExceptionHandler::class, ], - 'ws' => [ + 'ws' => [ App\Exception\Handler\WebSocketExceptionHandler::class ], ],