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\Validation\ValidationException;
11 use Symfony\Component\HttpKernel\Exception\HttpException;
12 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14 class Handler extends ExceptionHandler
17 * A list of the exception types that should not be reported.
21 protected $dontReport = [
22 AuthorizationException::class,
24 ModelNotFoundException::class,
25 ValidationException::class,
29 * Report or log an exception.
30 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
32 * @param \Exception $e
36 public function report(Exception $e)
38 return parent::report($e);
42 * Render an exception into an HTTP response.
44 * @param \Illuminate\Http\Request $request
45 * @param \Exception $e
46 * @return \Illuminate\Http\Response
48 public function render($request, Exception $e)
50 // Handle notify exceptions which will redirect to the
51 // specified location then show a notification message.
52 if ($this->isExceptionType($e, NotifyException::class)) {
53 session()->flash('error', $this->getOriginalMessage($e));
54 return redirect($e->redirectLocation);
57 // Handle pretty exceptions which will show a friendly application-fitting page
58 // Which will include the basic message to point the user roughly to the cause.
59 if ($this->isExceptionType($e, PrettyException::class) && !config('app.debug')) {
60 $message = $this->getOriginalMessage($e);
61 $code = ($e->getCode() === 0) ? 500 : $e->getCode();
62 return response()->view('errors/' . $code, ['message' => $message], $code);
65 // Handle 404 errors with a loaded session to enable showing user-specific information
66 if ($this->isExceptionType($e, NotFoundHttpException::class)) {
67 return \Route::respondWithRoute('fallback');
70 return parent::render($request, $e);
74 * Check the exception chain to compare against the original exception type.
79 protected function isExceptionType(Exception $e, $type)
82 if (is_a($e, $type)) {
85 } while ($e = $e->getPrevious());
90 * Get original exception message.
94 protected function getOriginalMessage(Exception $e)
97 $message = $e->getMessage();
98 } while ($e = $e->getPrevious());
103 * Convert an authentication exception into an unauthenticated response.
105 * @param \Illuminate\Http\Request $request
106 * @param \Illuminate\Auth\AuthenticationException $exception
107 * @return \Illuminate\Http\Response
109 protected function unauthenticated($request, AuthenticationException $exception)
111 if ($request->expectsJson()) {
112 return response()->json(['error' => 'Unauthenticated.'], 401);
115 return redirect()->guest('login');
119 * Convert a validation exception into a JSON response.
121 * @param \Illuminate\Http\Request $request
122 * @param \Illuminate\Validation\ValidationException $exception
123 * @return \Illuminate\Http\JsonResponse
125 protected function invalidJson($request, ValidationException $exception)
127 return response()->json($exception->errors(), $exception->status);