文件 App\Exceptions\Handler
此项目由于仅API,所以全部json返回,可根据项目需要调整。
public function render($request, Throwable $exception)
{
// 路由404异常监听
if($exception instanceof NotFoundHttpException){
$this->setHttpCode(404);
return $this->errorJson("路由{{$request->path()}}不存在!");
}
if ($exception instanceof ModelNotFoundException){
// var_dump('ModelNotFoundException');
// var_dump($exception->getMessage());
}
// 验证器类的错误监听
if($exception instanceof \Illuminate\Validation\ValidationException){
return $this->errorJson($exception->validator->errors()->first());
}
// Exception类的错误监听
if($exception instanceof \Exception){
return $this->errorJson($exception->getMessage(), $exception->getCode(), [], [
'file' => $exception->getFile(),
'line' => $exception->getLine(),
]);
}
return parent::render($request, $exception);
}
评论/回复