| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace App\Exceptions;
- use Exception;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Illuminate\Validation\ValidationException;
- use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
- class Handler extends ExceptionHandler
- {
- /**
- * A list of the exception types that are not reported.
- *
- * @var array
- */
- protected $dontReport = [
- //
- ];
- /**
- * A list of the inputs that are never flashed for validation exceptions.
- *
- * @var array
- */
- protected $dontFlash = [
- 'password',
- 'password_confirmation',
- ];
- /**
- * Report or log an exception.
- *
- * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
- *
- * @param \Exception $exception
- * @return void
- */
- public function report(Exception $exception)
- {
- parent::report($exception);
- }
- public function render($request, Exception $exception)
- {
- if ($exception instanceof ValidationException) {
- // return response(['error' => array_first(array_collapse($exception->errors()))], 400);
- return showJson(102, array_first(array_collapse($exception->errors())));
- }
- if ($exception instanceof UnauthorizedHttpException) {
- return response($exception->getMessage(), 401);
- }
- // 权限拦截器
- if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
- // 你的代码在这 ...
- return showJson(102, 1002, ['msg' => $exception->getMessage()]);
- }
- // 如果不被允许的路由
- if ($exception instanceof MethodNotAllowedHttpException || $exception instanceof NotFoundHttpException) {
- if (!($request->ajax() || $request->wantsJson())) {
- return showJson(102, '路由不存在');
- }
- }
- return parent::render($request, $exception);
- }
- }
|