From 2a1d03f8cf869fef671ff4388106a35415f55e14 Mon Sep 17 00:00:00 2001 From: "453530270@qq.com" Date: Mon, 7 Oct 2024 17:18:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BC=82=E5=B8=B8=E8=8E=B7?= =?UTF-8?q?=E8=A1=A5=E8=B4=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/ExceptionHandle.php | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/app/ExceptionHandle.php b/app/ExceptionHandle.php index 453d126..9b1c662 100644 --- a/app/ExceptionHandle.php +++ b/app/ExceptionHandle.php @@ -34,10 +34,20 @@ class ExceptionHandle extends Handle * @param Throwable $exception * @return void */ - public function report(Throwable $exception): void + public function report(Throwable $exception): Response { - // 使用内置的方式记录异常日志 - parent::report($exception); + // 手动触发的异常 BaseException + if ($e instanceof BaseException) { + $this->status = $e->status; + $this->message = $e->message; + $this->data = $e->data; + $extend = property_exists($e, 'extend') ? $e->extend : []; + return $this->output($extend); + }else{ + // 使用内置的方式记录异常日志 + parent::report($exception); + } + } /** @@ -55,4 +65,15 @@ class ExceptionHandle extends Handle // 其他错误交给系统处理 return parent::render($request, $e); } + + /** + * 返回json格式数据 + * @param array $extend 扩展的数据 + * @return Json + */ + private function output(array $extend = []): Json + { + $jsonData = ['message' => $this->message, 'status' => $this->status, 'data' => $this->data]; + return json(array_merge($jsonData, $extend)); + } }