]> BookStack Code Mirror - bookstack/blob - app/Exceptions/Handler.php
Updated not-found image path handling to have better ux
[bookstack] / app / Exceptions / Handler.php
1 <?php
2
3 namespace BookStack\Exceptions;
4
5 use Exception;
6 use Illuminate\Auth\AuthenticationException;
7 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8 use Illuminate\Http\JsonResponse;
9 use Illuminate\Http\Request;
10 use Illuminate\Validation\ValidationException;
11 use Symfony\Component\HttpKernel\Exception\HttpException;
12 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
14 class Handler extends ExceptionHandler
15 {
16     /**
17      * A list of the exception types that are not reported.
18      *
19      * @var array
20      */
21     protected $dontReport = [
22         NotFoundException::class,
23     ];
24
25     /**
26      * A list of the inputs that are never flashed for validation exceptions.
27      *
28      * @var array
29      */
30     protected $dontFlash = [
31         'password',
32         'password_confirmation',
33     ];
34
35     /**
36      * Report or log an exception.
37      *
38      * @param Exception $exception
39      * @return void
40      *
41      * @throws Exception
42      */
43     public function report(Exception $exception)
44     {
45         parent::report($exception);
46     }
47
48     /**
49      * Render an exception into an HTTP response.
50      *
51      * @param  \Illuminate\Http\Request $request
52      * @param Exception $e
53      * @return \Illuminate\Http\Response
54      */
55     public function render($request, Exception $e)
56     {
57         if ($this->isApiRequest($request)) {
58             return $this->renderApiException($e);
59         }
60
61         // Handle notify exceptions which will redirect to the
62         // specified location then show a notification message.
63         if ($this->isExceptionType($e, NotifyException::class)) {
64             $message = $this->getOriginalMessage($e);
65             if (!empty($message)) {
66                 session()->flash('error', $message);
67             }
68             return redirect($e->redirectLocation);
69         }
70
71         return parent::render($request, $e);
72     }
73
74     /**
75      * Check if the given request is an API request.
76      */
77     protected function isApiRequest(Request $request): bool
78     {
79         return strpos($request->path(), 'api/') === 0;
80     }
81
82     /**
83      * Render an exception when the API is in use.
84      */
85     protected function renderApiException(Exception $e): JsonResponse
86     {
87         $code = $e->getCode() === 0 ? 500 : $e->getCode();
88         $headers = [];
89         if ($e instanceof HttpException) {
90             $code = $e->getStatusCode();
91             $headers = $e->getHeaders();
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         $responseData['error']['code'] = $code;
106         return new JsonResponse($responseData, $code, $headers);
107     }
108
109     /**
110      * Check the exception chain to compare against the original exception type.
111      */
112     protected function isExceptionType(Exception $e, string $type): bool
113     {
114         do {
115             if (is_a($e, $type)) {
116                 return true;
117             }
118         } while ($e = $e->getPrevious());
119         return false;
120     }
121
122     /**
123      * Get original exception message.
124      */
125     protected function getOriginalMessage(Exception $e): string
126     {
127         do {
128             $message = $e->getMessage();
129         } while ($e = $e->getPrevious());
130         return $message;
131     }
132
133     /**
134      * Convert an authentication exception into an unauthenticated response.
135      *
136      * @param  \Illuminate\Http\Request  $request
137      * @param  \Illuminate\Auth\AuthenticationException  $exception
138      * @return \Illuminate\Http\Response
139      */
140     protected function unauthenticated($request, AuthenticationException $exception)
141     {
142         if ($request->expectsJson()) {
143             return response()->json(['error' => 'Unauthenticated.'], 401);
144         }
145
146         return redirect()->guest('login');
147     }
148
149     /**
150      * Convert a validation exception into a JSON response.
151      *
152      * @param  \Illuminate\Http\Request  $request
153      * @param  \Illuminate\Validation\ValidationException  $exception
154      * @return \Illuminate\Http\JsonResponse
155      */
156     protected function invalidJson($request, ValidationException $exception)
157     {
158         return response()->json($exception->errors(), $exception->status);
159     }
160 }