Browse Source

增加错误处理

dev
李可松 4 years ago
parent
commit
2a86c9bf79
  1. 39
      app/Exceptions/Handler.php

39
app/Exceptions/Handler.php

@ -3,7 +3,9 @@
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
use Illuminate\Validation\ValidationException;
class Handler extends ExceptionHandler
{
@ -38,4 +40,41 @@ class Handler extends ExceptionHandler
//
});
}
/**
* 报告异常
* @return bool|void
*/
public function report(Throwable $e): bool
{
// 判断异常是否需要自定义报告...
return false;
}
public function render($request, Throwable $e)
{
// 表单校验
if ($e instanceof ValidationException) {
$errors = $e->errors();
while (is_array($errors)) {
$errors = current($errors);
}
return response()->json([
'code' => -1,
'msg' => $errors,
'data' => [],
'status' => 500,
]);
}
// 404
if ($e instanceof NotFoundHttpException) {
return response()->json([
'code' => -1,
'msg' => '404 Not Found',
'data' => [],
'status' => 500,
]);
}
return parent::render($request, $e);
}
}
Loading…
Cancel
Save