From 407f93c89042dfa1124fea9ad4ab46448b78a6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 10 Sep 2020 13:42:08 +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 | 43 +++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index 948c3eaa..5f16fdb9 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -36,26 +36,13 @@ class OnRequest extends Callback public function onHandler(Request $request, Response $response) { try { + register_shutdown_function(OnRequest::class . '::shutdown', $response); + /** @var HRequest $sRequest */ [$sRequest, $sResponse] = static::setContext($request, $response); if ($sRequest->is('favicon.ico')) { return $params = $sResponse->send($sRequest->isNotFound(), 200); } - register_shutdown_function(function ($response) { - $error = error_get_last(); - if (!isset($error['type'])) { - return; - } - $types = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR]; - if (!in_array($error['type'], $types)) { - return; - } - if ($response instanceof Response) { - $response->status(500); - $response->end($error['message']); - } - unset($response); - }, $response); return $params = Snowflake::app()->getRouter()->dispatch(); } catch (Error | \Throwable $exception) { $params = $this->sendErrorMessage($sResponse ?? null, $exception, $response); @@ -68,6 +55,32 @@ class OnRequest extends Callback } } + /** + * @param $response + * @throws Exception + */ + public static function shutdown($response) + { + $error = error_get_last(); + if (!isset($error['type'])) { + return; + } + $types = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR]; + if (!in_array($error['type'], $types)) { + return; + } + try { + $message = $error['message'] . ':' . microtime(true); + if ($response instanceof Response) { + $response->status(500); + $response->end($message); + } + } catch (\ErrorException $exception) { + $logger = Snowflake::app()->logger; + $logger->write($exception->getMessage(), 'shutdown'); + } + unset($response); + } /** * @param $sResponse