Browse Source

报错方法

lizhen_gitee 1 month ago
parent
commit
a403c8d9ee

+ 26 - 28
application/api/library/ExceptionHandle.php

@@ -14,40 +14,38 @@ class ExceptionHandle extends Handle
 
     public function render(Exception $e)
     {
+        return parent::render($e);
+
         // 在生产环境下返回code信息
-        if (!\think\Config::get('api_exception')) {
-            $statuscode = $code = 500;
+        $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();
-            // 验证异常
-            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];
-
-            if (defined('API_REQUEST_LOG_TYPE') && defined('API_REQUEST_LOG_TYPE') == 1){
-                LogUtil::error('result','Api-Middleware-Log','request_log',$result);
-                LogUtil::error('ExceptionMsg','Api-Middleware-Log','request_log',$e->getMessage());
-                LogUtil::error('Exception','Api-Middleware-Log','request_log',$e);
-            }else{
-                //记录app异常返回结果
-                if(defined('API_REQUEST_ID')) { //记录app正常返回结果
-                    db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($result)]);
-                }
+        $result = ['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null];
+
+        if (defined('API_REQUEST_LOG_TYPE') && defined('API_REQUEST_LOG_TYPE') == 1){
+            LogUtil::error('result','Api-Middleware-Log','request_log',$result);
+            LogUtil::error('ExceptionMsg','Api-Middleware-Log','request_log',$e->getMessage());
+            LogUtil::error('Exception','Api-Middleware-Log','request_log',$e);
+        }else{
+            //记录app异常返回结果
+            if(defined('API_REQUEST_ID')) { //记录app正常返回结果
+                db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($result)]);
             }
-            $result['msg'] = '网络开小差了';
-            return json($result, $statuscode);
         }
+        //$result['msg'] = '网络开小差了';
+        return json($result, $statuscode);
 
-        //其它此交由系统处理
-        return parent::render($e);
     }
 
 }

+ 26 - 28
application/company/library/ExceptionHandle.php

@@ -14,40 +14,38 @@ class ExceptionHandle extends Handle
 
     public function render(Exception $e)
     {
+        return parent::render($e);
+
         // 在生产环境下返回code信息
-        if (!\think\Config::get('api_exception')) {
-            $statuscode = $code = 500;
+        $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();
-            // 验证异常
-            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];
-
-            if (defined('API_REQUEST_LOG_TYPE') && defined('API_REQUEST_LOG_TYPE') == 1){
-                LogUtil::error('result','Api-Middleware-Log','request_log',$result);
-                LogUtil::error('ExceptionMsg','Api-Middleware-Log','request_log',$e->getMessage());
-                LogUtil::error('Exception','Api-Middleware-Log','request_log',$e);
-            }else{
-                //记录app异常返回结果
-                if(defined('API_REQUEST_ID')) { //记录app正常返回结果
-                    db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($result)]);
-                }
+        $result = ['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null];
+
+        if (defined('API_REQUEST_LOG_TYPE') && defined('API_REQUEST_LOG_TYPE') == 1){
+            LogUtil::error('result','Api-Middleware-Log','request_log',$result);
+            LogUtil::error('ExceptionMsg','Api-Middleware-Log','request_log',$e->getMessage());
+            LogUtil::error('Exception','Api-Middleware-Log','request_log',$e);
+        }else{
+            //记录app异常返回结果
+            if(defined('API_REQUEST_ID')) { //记录app正常返回结果
+                db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($result)]);
             }
-            $result['msg'] = '网络开小差了';
-            return json($result, $statuscode);
         }
+        //$result['msg'] = '网络开小差了';
+        return json($result, $statuscode);
 
-        //其它此交由系统处理
-        return parent::render($e);
     }
 
 }