]> BookStack Code Mirror - bookstack/blob - app/Exceptions/Handler.php
Merge branch 'fix-api-404' into development
[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\HttpExceptionInterface;
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 HttpExceptionInterface) {
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']['message'] = 'The given data was invalid.';
102             $responseData['error']['validation'] = $e->errors();
103             $code = $e->status;
104         }
105
106         $responseData['error']['code'] = $code;
107
108         return new JsonResponse($responseData, $code, $headers);
109     }
110
111     /**
112      * Convert an authentication exception into an unauthenticated response.
113      *
114      * @param \Illuminate\Http\Request                 $request
115      * @param \Illuminate\Auth\AuthenticationException $exception
116      *
117      * @return \Illuminate\Http\Response
118      */
119     protected function unauthenticated($request, AuthenticationException $exception)
120     {
121         if ($request->expectsJson()) {
122             return response()->json(['error' => 'Unauthenticated.'], 401);
123         }
124
125         return redirect()->guest('login');
126     }
127
128     /**
129      * Convert a validation exception into a JSON response.
130      *
131      * @param \Illuminate\Http\Request                   $request
132      * @param \Illuminate\Validation\ValidationException $exception
133      *
134      * @return \Illuminate\Http\JsonResponse
135      */
136     protected function invalidJson($request, ValidationException $exception)
137     {
138         return response()->json($exception->errors(), $exception->status);
139     }
140 }