From c82b6ade42c6424ec21492dde23520423d065b83 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Sat, 4 Jan 2025 12:09:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor(exception):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除自定义 MyBusinessException 类 - 统一使用 support\exception\BusinessException - 在 ExceptionHandler 中增加日志记录 - 更新相关文件中的异常处理逻辑 --- app/ExceptionHandler.php | 5 +++++ app/MyBusinessException.php | 20 ------------------- app/admin/validate/LoginValidate.php | 1 - app/api/lists/store/SystemStoreLists.php | 3 +-- app/store/logic/LoginLogic.php | 2 -- .../StoreBranchProductLogic.php | 1 - 6 files changed, 6 insertions(+), 26 deletions(-) delete mode 100644 app/MyBusinessException.php diff --git a/app/ExceptionHandler.php b/app/ExceptionHandler.php index 483b9d6b..0ba7badc 100644 --- a/app/ExceptionHandler.php +++ b/app/ExceptionHandler.php @@ -7,6 +7,7 @@ use Next\VarDumper\Dumper; use Next\VarDumper\DumperHandler; use support\exception\BusinessException; use support\exception\Handler; +use support\Log; use Throwable; use Webman\Http\Request; use Webman\Http\Response; @@ -22,6 +23,7 @@ class ExceptionHandler extends Handler return \response(self::convertToHtml($exception)); }elseif ($exception instanceof BusinessException) { if ($request->expectsJson()) { + Log::error('BusinessException:',['msg'=>$exception->getMessage(),'file'=>$exception->getFile(),'line'=>$exception->getLine()]); return json(['code' => 0, 'msg' => $exception->getMessage(),'show'=>1]); } return response($exception->getMessage()); @@ -35,9 +37,12 @@ class ExceptionHandler extends Handler $error['file'] = $exception->getFile(); $error['line'] = $exception->getLine(); } + Log::error('Exception:',['msg'=>$exception->getMessage(),'file'=>$exception->getFile(),'line'=>$exception->getLine()]); return response(json_encode($error, JSON_UNESCAPED_UNICODE)); } // 非json请求则返回一个页面 + Log::error('other:',['msg'=>$exception->getMessage(),'file'=>$exception->getFile(),'line'=>$exception->getLine()]); + return new Response(200, [], 'msg:'.$exception->getMessage().'。line:'.$exception->getLine().'。file:'.$exception->getFile()); } } diff --git a/app/MyBusinessException.php b/app/MyBusinessException.php deleted file mode 100644 index bebfa4e0..00000000 --- a/app/MyBusinessException.php +++ /dev/null @@ -1,20 +0,0 @@ -expectsJson()) { - return json(['code' => $this->getCode() ?: 500, 'msg' => $this->getMessage(),'show'=>1]); - } - // 非json请求则返回一个页面 - return new Response(200, [], $this->getMessage()); - } -} \ No newline at end of file diff --git a/app/admin/validate/LoginValidate.php b/app/admin/validate/LoginValidate.php index 02dac88a..5b39f448 100644 --- a/app/admin/validate/LoginValidate.php +++ b/app/admin/validate/LoginValidate.php @@ -9,7 +9,6 @@ use app\common\enum\AdminTerminalEnum; use app\common\model\auth\Admin; use app\common\service\ConfigService; use app\common\validate\BaseValidate; -use app\MyBusinessException; use support\exception\BusinessException; use Webman\Config; diff --git a/app/api/lists/store/SystemStoreLists.php b/app/api/lists/store/SystemStoreLists.php index a45511f3..b48eaae6 100644 --- a/app/api/lists/store/SystemStoreLists.php +++ b/app/api/lists/store/SystemStoreLists.php @@ -9,8 +9,7 @@ use app\common\model\order\Cart; use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\system_store\SystemStore; use app\common\lists\ListsSearchInterface; -use app\MyBusinessException; -use think\Exception; + /** diff --git a/app/store/logic/LoginLogic.php b/app/store/logic/LoginLogic.php index 453a991c..adec25c4 100644 --- a/app/store/logic/LoginLogic.php +++ b/app/store/logic/LoginLogic.php @@ -20,8 +20,6 @@ use app\common\model\system_store\SystemStoreStaff; use app\store\service\AdminTokenService; use app\common\model\auth\AdminRole; use app\common\service\FileService; -use app\MyBusinessException; -use think\facade\Db; use Webman\Config; /** diff --git a/app/store/logic/store_branch_product/StoreBranchProductLogic.php b/app/store/logic/store_branch_product/StoreBranchProductLogic.php index 7837ac1b..9b1bce6c 100644 --- a/app/store/logic/store_branch_product/StoreBranchProductLogic.php +++ b/app/store/logic/store_branch_product/StoreBranchProductLogic.php @@ -9,7 +9,6 @@ use app\common\model\store_product\StoreProduct; use app\common\logic\BaseLogic; use app\common\model\store_category\StoreCategory; use app\common\model\store_product_attr_value\StoreProductAttrValue; -use app\MyBusinessException; use support\exception\BusinessException; use think\facade\Db;