]> BookStack Code Mirror - bookstack/blob - app/Exceptions/Handler.php
Fixed user invite email subject in spanish translation (#2608)
[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         // Handle pretty exceptions which will show a friendly application-fitting page
72         // Which will include the basic message to point the user roughly to the cause.
73         if ($this->isExceptionType($e, PrettyException::class)  && !config('app.debug')) {
74             $message = $this->getOriginalMessage($e);
75             $code = ($e->getCode() === 0) ? 500 : $e->getCode();
76             return response()->view('errors/' . $code, ['message' => $message], $code);
77         }
78
79         // Handle 404 errors with a loaded session to enable showing user-specific information
80         if ($this->isExceptionType($e, NotFoundHttpException::class)) {
81             return \Route::respondWithRoute('fallback');
82         }
83
84         return parent::render($request, $e);
85     }
86
87     /**
88      * Check if the given request is an API request.
89      */
90     protected function isApiRequest(Request $request): bool
91     {
92         return strpos($request->path(), 'api/') === 0;
93     }
94
95     /**
96      * Render an exception when the API is in use.
97      */
98     protected function renderApiException(Exception $e): JsonResponse
99     {
100         $code = $e->getCode() === 0 ? 500 : $e->getCode();
101         $headers = [];
102         if ($e instanceof HttpException) {
103             $code = $e->getStatusCode();
104             $headers = $e->getHeaders();
105         }
106
107         $responseData = [
108             'error' => [
109                 'message' => $e->getMessage(),
110             ]
111         ];
112
113         if ($e instanceof ValidationException) {
114             $responseData['error']['validation'] = $e->errors();
115             $code = $e->status;
116         }
117
118         $responseData['error']['code'] = $code;
119         return new JsonResponse($responseData, $code, $headers);
120     }
121
122     /**
123      * Check the exception chain to compare against the original exception type.
124      */
125     protected function isExceptionType(Exception $e, string $type): bool
126     {
127         do {
128             if (is_a($e, $type)) {
129                 return true;
130             }
131         } while ($e = $e->getPrevious());
132         return false;
133     }
134
135     /**
136      * Get original exception message.
137      */
138     protected function getOriginalMessage(Exception $e): string
139     {
140         do {
141             $message = $e->getMessage();
142         } while ($e = $e->getPrevious());
143         return $message;
144     }
145
146     /**
147      * Convert an authentication exception into an unauthenticated response.
148      *
149      * @param  \Illuminate\Http\Request  $request
150      * @param  \Illuminate\Auth\AuthenticationException  $exception
151      * @return \Illuminate\Http\Response
152      */
153     protected function unauthenticated($request, AuthenticationException $exception)
154     {
155         if ($request->expectsJson()) {
156             return response()->json(['error' => 'Unauthenticated.'], 401);
157         }
158
159         return redirect()->guest('login');
160     }
161
162     /**
163      * Convert a validation exception into a JSON response.
164      *
165      * @param  \Illuminate\Http\Request  $request
166      * @param  \Illuminate\Validation\ValidationException  $exception
167      * @return \Illuminate\Http\JsonResponse
168      */
169     protected function invalidJson($request, ValidationException $exception)
170     {
171         return response()->json($exception->errors(), $exception->status);
172     }
173 }