From 2750469159779902f138c25733ca956c7ce804b0 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Fri, 16 Jul 2021 22:15:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Exception/BaseException.php | 16 ++++++++ .../Handler/BaseExceptionHandler.php | 37 +++++++++++++++++++ .../Handler/JwtAuthExceptionHandler.php | 1 - .../Handler/ValidateExceptionHandler.php | 1 - config/autoload/exceptions.php | 3 +- 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 app/Exception/BaseException.php create mode 100644 app/Exception/Handler/BaseExceptionHandler.php 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 ], ],