1
0
Fork 0
medical-record-server/app/Exceptions/Handler.php

63 lines
2.0 KiB
PHP

<?php
namespace App\Exceptions;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Handler extends ExceptionHandler
{
/**
* The list of the inputs that are never flashed to the session on validation exceptions.
*
* @var array<int, string>
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
protected $dontReport = [
BaseException::class,
];
/**
* Register the exception handling callbacks for the application.
*/
public function register(): void
{
$this->renderable(function (NotFoundHttpException $e, Request $request) {
if ($this->shouldReturnJson($request, $e)) {
return response()->json(['status' => 1, 'msg' => '资源不存在', 'data' => '']);
}
});
$this->renderable(function (BaseException $e, Request $request) {
if ($this->shouldReturnJson($request, $e)) {
return response()->json(['status' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => '']);
}
});
}
protected function unauthenticated($request, AuthenticationException $exception)
{
return $this->shouldReturnJson($request, $exception)
? response()->json(['status' => 401, 'msg' => $exception->getMessage(), 'data' => ''])
: redirect()->guest($exception->redirectTo() ?? route('login'));
}
protected function invalidJson($request, ValidationException $exception)
{
return response()->json([
'status' => 422,
'msg' => str_replace(' (and 1 more error)', '', $exception->getMessage()),
'errors' => $exception->errors(),
'data' => '',
], 200);
}
}