From 53cbc714ed586ce6f1aaf832c49fb8e166fe2bcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 8 Jun 2021 13:55:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Events/OnRequest.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index 4934ccb7..e1e7048e 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -16,6 +16,7 @@ use Snowflake\Snowflake; use Swoole\Error; use Swoole\Http\Request; use Swoole\Http\Response; +use Throwable; /** * Class OnRequest @@ -59,7 +60,7 @@ class OnRequest extends Callback return $response->close(404); } return $this->router->dispatch(); - } catch (ExitException | Error | \Throwable $exception) { + } catch (ExitException | Error | Throwable $exception) { $this->addError($exception, 'throwable'); return $this->sendErrorMessage($request, $response, $exception); } @@ -81,11 +82,11 @@ class OnRequest extends Callback /** * @param $sRequest * @param $sResponse - * @param $exception + * @param Throwable $exception * @return bool|string * @throws Exception */ - protected function sendErrorMessage($sRequest, $sResponse, $exception): bool|string + protected function sendErrorMessage($sRequest, $sResponse, Throwable $exception): bool|string { $this->addError($exception, 'throwable'); if ($sResponse instanceof Response) { @@ -95,14 +96,16 @@ class OnRequest extends Callback $headers = $sRequest->headers->get('access-control-request-headers'); $methods = $sRequest->headers->get('access-control-request-method'); + /** @var HResponse $sResponse */ $sResponse->addHeader('Access-Control-Allow-Origin', '*'); $sResponse->addHeader('Access-Control-Allow-Headers', $headers); $sResponse->addHeader('Access-Control-Request-Method', $methods); + return $sResponse->send($exception->getMessage(), $exception->getCode()); + if (!($exception instanceof ExitException)) { return $sResponse->send(\logger()->exception($exception), 200); } else { - return $sResponse->send($exception->getMessage(), $exception?->getCode() ?? 200); } }