From 2cbc28bc47592e14e813fbb3d52227fe8b73e7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Sun, 14 Feb 2021 22:00:05 +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/Route/Node.php | 1 - HttpServer/Route/Reduce.php | 13 ++++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index de595883..92979753 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -106,7 +106,6 @@ class Node extends Application { return Coroutine::create(function ($request, $response) { (Reduce::after($this->_after))($request, $response); - fire(Event::EVENT_AFTER_REQUEST); }, \request(), $response); } diff --git a/HttpServer/Route/Reduce.php b/HttpServer/Route/Reduce.php index 8b373bdf..79b0a700 100644 --- a/HttpServer/Route/Reduce.php +++ b/HttpServer/Route/Reduce.php @@ -7,6 +7,8 @@ namespace HttpServer\Route; use Closure; use HttpServer\IInterface\After; use HttpServer\IInterface\Middleware; +use Snowflake\Core\Json; +use Snowflake\Event; class Reduce { @@ -31,10 +33,15 @@ class Reduce { return array_reduce(array_reverse($middleWares), function ($stack, $pipe) { return function ($request, $passable) use ($stack, $pipe) { - if ($pipe instanceof After) { + try { + if (!($pipe instanceof After)) { + return call_user_func($pipe, $request, $passable, $stack); + } return $pipe->onHandler($request, $passable); - } else { - return call_user_func($pipe, $request, $passable, $stack); + } catch (\Throwable $throwable) { + return Json::to(0, $throwable); + } finally { + fire(Event::EVENT_AFTER_REQUEST); } }; });