reportable(function (Throwable $e) { })->stop(); } /** * 将异常呈现到HTTP响应中 * * * @return \Illuminate\Http\JsonResponse */ public function render($request, Throwable $e){ // 如果不是接口, if( !$request->is('api/*') ) { // 如果是后端验证类 if( $e instanceof AuthException ) return $e->jumpPage(); // 如果不是验证返回,调用父级异常呈现 if( !($e instanceof ApiException) ) return parent::render($request, $e); } // 如果是异常类型,获取异常的消息 $msg = $e->getMessage(); // 错误信息 $msg = $msg ? $msg : '404'; // 错误数据 $data = ''; // 错误码 $code = 'sys_error'; // http状态码 $status = 200; // 日志信息 $message = request()->ip().' ' .request()->method().' '. request()->getPathInfo().' '.$msg; // 如果是接口异常 if( $e instanceof ApiException ){ // 错误码 $code = $e->getErrCode(); // 错误数据 $data = $e->getErrData(); }else{ // 拼接报错文件位置 $message .= ' of file '.$e->getFile().' on line '.$e->getLine(); } // 要返回的数据 $data = ['code'=>($code == 'sys_error' ? 'error' : $code),'msg'=>$msg,'data'=>$data]; // 记录日志 Log::info($code,$message,['param'=>request()->all(),'return'=>$data,'user_agent'=>request()->header('user-agent')],['filename'=>str_ireplace('/','_',trim(request()->getPathInfo(),'/'))]); // 返回结果 return response()->json($data,$status,[],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE); } }