3 namespace BookStack\Exceptions;
6 use Illuminate\Auth\AuthenticationException;
7 use Illuminate\Validation\ValidationException;
8 use Illuminate\Database\Eloquent\ModelNotFoundException;
9 use Symfony\Component\HttpKernel\Exception\HttpException;
10 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
11 use Illuminate\Auth\Access\AuthorizationException;
13 class Handler extends ExceptionHandler
16 * A list of the exception types that should not be reported.
20 protected $dontReport = [
21 AuthorizationException::class,
23 ModelNotFoundException::class,
24 ValidationException::class,
28 * Report or log an exception.
29 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
31 * @param \Exception $e
34 public function report(Exception $e)
36 return parent::report($e);
40 * Render an exception into an HTTP response.
42 * @param \Illuminate\Http\Request $request
43 * @param \Exception $e
44 * @return \Illuminate\Http\Response
46 public function render($request, Exception $e)
48 // Handle notify exceptions which will redirect to the
49 // specified location then show a notification message.
50 if ($this->isExceptionType($e, NotifyException::class)) {
51 session()->flash('error', $this->getOriginalMessage($e));
52 return redirect($e->redirectLocation);
55 // Handle pretty exceptions which will show a friendly application-fitting page
56 // Which will include the basic message to point the user roughly to the cause.
57 if ($this->isExceptionType($e, PrettyException::class) && !config('app.debug')) {
58 $message = $this->getOriginalMessage($e);
59 $code = ($e->getCode() === 0) ? 500 : $e->getCode();
60 return response()->view('errors/' . $code, ['message' => $message], $code);
63 return parent::render($request, $e);
67 * Check the exception chain to compare against the original exception type.
72 protected function isExceptionType(Exception $e, $type) {
74 if (is_a($e, $type)) return true;
75 } while ($e = $e->getPrevious());
80 * Get original exception message.
84 protected function getOriginalMessage(Exception $e) {
86 $message = $e->getMessage();
87 } while ($e = $e->getPrevious());
92 * Convert an authentication exception into an unauthenticated response.
94 * @param \Illuminate\Http\Request $request
95 * @param \Illuminate\Auth\AuthenticationException $exception
96 * @return \Illuminate\Http\Response
98 protected function unauthenticated($request, AuthenticationException $exception)
100 if ($request->expectsJson()) {
101 return response()->json(['error' => 'Unauthenticated.'], 401);
104 return redirect()->guest('login');
108 * Convert a validation exception into a JSON response.
110 * @param \Illuminate\Http\Request $request
111 * @param \Illuminate\Validation\ValidationException $exception
112 * @return \Illuminate\Http\JsonResponse
114 protected function invalidJson($request, ValidationException $exception)
116 return response()->json($exception->errors(), $exception->status);