3 namespace BookStack\Exceptions;
6 use Illuminate\Auth\Access\AuthorizationException;
7 use Illuminate\Auth\AuthenticationException;
8 use Illuminate\Database\Eloquent\ModelNotFoundException;
9 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
10 use Illuminate\Http\JsonResponse;
11 use Illuminate\Http\Request;
12 use Illuminate\Http\Response;
13 use Illuminate\Validation\ValidationException;
14 use Symfony\Component\HttpKernel\Exception\HttpException;
15 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17 class Handler extends ExceptionHandler
20 * A list of the exception types that should not be reported.
24 protected $dontReport = [
25 AuthorizationException::class,
27 ModelNotFoundException::class,
28 ValidationException::class,
32 * Report or log an exception.
33 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
35 * @param \Exception $e
39 public function report(Exception $e)
41 return parent::report($e);
45 * Render an exception into an HTTP response.
47 * @param \Illuminate\Http\Request $request
48 * @param \Exception $e
49 * @return \Illuminate\Http\Response
51 public function render($request, Exception $e)
53 if ($this->isApiRequest($request)) {
54 return $this->renderApiException($e);
57 // Handle notify exceptions which will redirect to the
58 // specified location then show a notification message.
59 if ($this->isExceptionType($e, NotifyException::class)) {
60 session()->flash('error', $this->getOriginalMessage($e));
61 return redirect($e->redirectLocation);
64 // Handle pretty exceptions which will show a friendly application-fitting page
65 // Which will include the basic message to point the user roughly to the cause.
66 if ($this->isExceptionType($e, PrettyException::class) && !config('app.debug')) {
67 $message = $this->getOriginalMessage($e);
68 $code = ($e->getCode() === 0) ? 500 : $e->getCode();
69 return response()->view('errors/' . $code, ['message' => $message], $code);
72 // Handle 404 errors with a loaded session to enable showing user-specific information
73 if ($this->isExceptionType($e, NotFoundHttpException::class)) {
74 return \Route::respondWithRoute('fallback');
77 return parent::render($request, $e);
81 * Check if the given request is an API request.
83 protected function isApiRequest(Request $request): bool
85 return strpos($request->path(), 'api/') === 0;
89 * Render an exception when the API is in use.
91 protected function renderApiException(Exception $e): JsonResponse
93 $code = $e->getCode() === 0 ? 500 : $e->getCode();
95 if ($e instanceof HttpException) {
96 $code = $e->getStatusCode();
97 $headers = $e->getHeaders();
102 'message' => $e->getMessage(),
106 if ($e instanceof ValidationException) {
107 $responseData['error']['validation'] = $e->errors();
111 $responseData['error']['code'] = $code;
112 return new JsonResponse($responseData, $code, $headers);
116 * Check the exception chain to compare against the original exception type.
117 * @param Exception $e
121 protected function isExceptionType(Exception $e, $type)
124 if (is_a($e, $type)) {
127 } while ($e = $e->getPrevious());
132 * Get original exception message.
133 * @param Exception $e
136 protected function getOriginalMessage(Exception $e)
139 $message = $e->getMessage();
140 } while ($e = $e->getPrevious());
145 * Convert an authentication exception into an unauthenticated response.
147 * @param \Illuminate\Http\Request $request
148 * @param \Illuminate\Auth\AuthenticationException $exception
149 * @return \Illuminate\Http\Response
151 protected function unauthenticated($request, AuthenticationException $exception)
153 if ($request->expectsJson()) {
154 return response()->json(['error' => 'Unauthenticated.'], 401);
157 return redirect()->guest('login');
161 * Convert a validation exception into a JSON response.
163 * @param \Illuminate\Http\Request $request
164 * @param \Illuminate\Validation\ValidationException $exception
165 * @return \Illuminate\Http\JsonResponse
167 protected function invalidJson($request, ValidationException $exception)
169 return response()->json($exception->errors(), $exception->status);