示例#1
1
 /**
  * Render an exception into an HTTP response.
  * 该方法响应异常信息到客户端 : 该方法返回的数据必须是一个response对象
  * 异常的处理是在请求结束前处理,因此这里的render 方法直接设定为返回异常信息给响应。
  * @param  \Illuminate\Http\Request  $request
  * @param  \Exception  $e
  * @return \Illuminate\Http\Response
  */
 public function render($request, Exception $e)
 {
     if ($e instanceof ModelNotFoundException) {
         $e = new NotFoundHttpException($e->getMessage(), $e);
     }
     $msg = $e->getmessage();
     if ($e instanceof InvalidArgumentException) {
         return response()->json(['data' => $msg, 'state' => '901', 'timeStamp' => time()]);
     }
     if ($e instanceof UnexpectedValueException) {
         return response()->json(['data' => $msg, 'state' => '902', 'timeStamp' => time()]);
     }
     if ($e instanceof SignatureInvalidException) {
         return response()->json(['data' => $msg, 'state' => '903', 'timeStamp' => time()]);
         // return response()->view('welcome', [], 503);
         // return  response("ddddd")->header("x-at",'xxx');
     }
     if ($e instanceof BeforeValidException) {
         return response()->json(['data' => $msg, 'state' => '904', 'timeStamp' => time()]);
     }
     if ($e instanceof ExpiredException) {
         return response()->json(['data' => $msg, 'state' => '905', 'timeStamp' => time()]);
     }
     if ($e instanceof CustomException) {
         return response()->json(['data' => $msg, 'state' => '1005', 'timeStamp' => time()]);
     }
     return parent::render($request, $e);
 }