]> BookStack Code Mirror - bookstack/blob - app/Exceptions/Handler.php
Followed Laravel 9 update steps and file changes
[bookstack] / app / Exceptions / Handler.php
1 <?php
2
3 namespace BookStack\Exceptions;
4
5 use Exception;
6 use Illuminate\Auth\AuthenticationException;
7 use Illuminate\Database\Eloquent\ModelNotFoundException;
8 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
9 use Illuminate\Http\JsonResponse;
10 use Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
12 use Symfony\Component\HttpKernel\Exception\HttpException;
13 use Throwable;
14
15 class Handler extends ExceptionHandler
16 {
17     /**
18      * A list of the exception types that are not reported.
19      *
20      * @var array<int, class-string<\Throwable>>
21      */
22     protected $dontReport = [
23         NotFoundException::class,
24         StoppedAuthenticationException::class,
25     ];
26
27     /**
28      * A list of the inputs that are never flashed to the session on validation exceptions.
29      *
30      * @var array<int, string>
31      */
32     protected $dontFlash = [
33         'current_password',
34         'password',
35         'password_confirmation',
36     ];
37
38     /**
39      * Report or log an exception.
40      *
41      * @param \Throwable $exception
42      *
43      * @throws \Throwable
44      *
45      * @return void
46      */
47     public function report(Throwable $exception)
48     {
49         parent::report($exception);
50     }
51
52     /**
53      * Render an exception into an HTTP response.
54      *
55      * @param \Illuminate\Http\Request $request
56      * @param Exception                $e
57      *
58      * @return \Illuminate\Http\Response
59      */
60     public function render($request, Throwable $e)
61     {
62         if ($this->isApiRequest($request)) {
63             return $this->renderApiException($e);
64         }
65
66         return parent::render($request, $e);
67     }
68
69     /**
70      * Check if the given request is an API request.
71      */
72     protected function isApiRequest(Request $request): bool
73     {
74         return strpos($request->path(), 'api/') === 0;
75     }
76
77     /**
78      * Render an exception when the API is in use.
79      */
80     protected function renderApiException(Throwable $e): JsonResponse
81     {
82         $code = 500;
83         $headers = [];
84
85         if ($e instanceof HttpException) {
86             $code = $e->getStatusCode();
87             $headers = $e->getHeaders();
88         }
89
90         if ($e instanceof ModelNotFoundException) {
91             $code = 404;
92         }
93
94         $responseData = [
95             'error' => [
96                 'message' => $e->getMessage(),
97             ],
98         ];
99
100         if ($e instanceof ValidationException) {
101             $responseData['error']['validation'] = $e->errors();
102             $code = $e->status;
103         }
104
105         if (method_exists($e, 'getStatus')) {
106             $code = $e->getStatus();
107         }
108
109         $responseData['error']['code'] = $code;
110
111         return new JsonResponse($responseData, $code, $headers);
112     }
113
114     /**
115      * Convert an authentication exception into an unauthenticated response.
116      *
117      * @param \Illuminate\Http\Request                 $request
118      * @param \Illuminate\Auth\AuthenticationException $exception
119      *
120      * @return \Illuminate\Http\Response
121      */
122     protected function unauthenticated($request, AuthenticationException $exception)
123     {
124         if ($request->expectsJson()) {
125             return response()->json(['error' => 'Unauthenticated.'], 401);
126         }
127
128         return redirect()->guest('login');
129     }
130
131     /**
132      * Convert a validation exception into a JSON response.
133      *
134      * @param \Illuminate\Http\Request                   $request
135      * @param \Illuminate\Validation\ValidationException $exception
136      *
137      * @return \Illuminate\Http\JsonResponse
138      */
139     protected function invalidJson($request, ValidationException $exception)
140     {
141         return response()->json($exception->errors(), $exception->status);
142     }
143 }