From e88a15a30098735958f876a91778caf394f28bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 16 Dec 2020 15:25:09 +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/Middleware.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/HttpServer/Route/Middleware.php b/HttpServer/Route/Middleware.php index 0d63b875..6cdee8b3 100644 --- a/HttpServer/Route/Middleware.php +++ b/HttpServer/Route/Middleware.php @@ -15,6 +15,7 @@ use Annotation\Route\Middleware as RMiddleware; use Exception; use Annotation\Route\Limits; use HttpServer\Route\Dispatch\Dispatch; +use Snowflake\Core\JSON; use Snowflake\Snowflake; /** @@ -54,12 +55,21 @@ class Middleware */ public function getGenerate(Node $node): mixed { - if (is_array($node->handler) && is_object($node->handler[0])) { - $this->set_attributes($node); + try { + if (is_array($node->handler) && is_object($node->handler[0])) { + $this->set_attributes($node); + } + return $node->callback = Reduce::reduce(function () use ($node) { + return Dispatch::create($node->handler, func_get_args())->dispatch(); + }, $this->annotation($node)); + } catch (\Throwable $exception) { + return $node->callback = function () use ($exception) { + return JSON::to(500, $exception->getMessage(), [ + 'file' => $exception->getFile(), + 'line' => $exception->getLine() + ]); + }; } - return $node->callback = Reduce::reduce(function () use ($node) { - return Dispatch::create($node->handler, func_get_args())->dispatch(); - }, $this->annotation($node)); }