|
@@ -0,0 +1,46 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\admin\library;
|
|
|
+
|
|
|
+use Exception;
|
|
|
+use think\exception\Handle;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 自定义API模块的错误显示
|
|
|
+ */
|
|
|
+class ExceptionHandle extends Handle
|
|
|
+{
|
|
|
+
|
|
|
+ public function render(Exception $e)
|
|
|
+ {
|
|
|
+ //return parent::render($e);
|
|
|
+ $statuscode = $code = 500;
|
|
|
+ $msg = $e->getMessage();
|
|
|
+ // 验证异常
|
|
|
+ if ($e instanceof \think\exception\ValidateException) {
|
|
|
+ $code = 0;
|
|
|
+ $statuscode = 200;
|
|
|
+ $msg = $e->getError();
|
|
|
+
|
|
|
+ }
|
|
|
+ // Http异常
|
|
|
+ if ($e instanceof \think\exception\HttpException) {
|
|
|
+ $statuscode = $code = $e->getStatusCode();
|
|
|
+ $msg = $e->getMessage();
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = ['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null];
|
|
|
+
|
|
|
+
|
|
|
+ //记录app异常返回结果
|
|
|
+ $modulename = request()->module();
|
|
|
+ $controllername = request()->controller();
|
|
|
+ $actionname = request()->action();
|
|
|
+
|
|
|
+ db('admin_request_log')->insert(['api' => $modulename.'/'.$controllername.'/'.$actionname,'params'=>json_encode(request()->request()), 'result'=>json_encode($result)]);
|
|
|
+
|
|
|
+
|
|
|
+ return json($result, $statuscode);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|