3 namespace BookStack\Exceptions;
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;
14 class Handler extends ExceptionHandler
17 * A list of the exception types that are not reported.
21 protected $dontReport = [
22 NotFoundException::class,
26 * A list of the inputs that are never flashed for validation exceptions.
30 protected $dontFlash = [
32 'password_confirmation',
36 * Report or log an exception.
38 * @param Exception $exception
43 public function report(Exception $exception)
45 parent::report($exception);
49 * Render an exception into an HTTP response.
51 * @param \Illuminate\Http\Request $request
53 * @return \Illuminate\Http\Response
55 public function render($request, Exception $e)
57 if ($this->isApiRequest($request)) {
58 return $this->renderApiException($e);
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);
68 return redirect($e->redirectLocation);
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);
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');
84 return parent::render($request, $e);
88 * Check if the given request is an API request.
90 protected function isApiRequest(Request $request): bool
92 return strpos($request->path(), 'api/') === 0;
96 * Render an exception when the API is in use.
98 protected function renderApiException(Exception $e): JsonResponse
100 $code = $e->getCode() === 0 ? 500 : $e->getCode();
102 if ($e instanceof HttpException) {
103 $code = $e->getStatusCode();
104 $headers = $e->getHeaders();
109 'message' => $e->getMessage(),
113 if ($e instanceof ValidationException) {
114 $responseData['error']['validation'] = $e->errors();
118 $responseData['error']['code'] = $code;
119 return new JsonResponse($responseData, $code, $headers);
123 * Check the exception chain to compare against the original exception type.
125 protected function isExceptionType(Exception $e, string $type): bool
128 if (is_a($e, $type)) {
131 } while ($e = $e->getPrevious());
136 * Get original exception message.
138 protected function getOriginalMessage(Exception $e): string
141 $message = $e->getMessage();
142 } while ($e = $e->getPrevious());
147 * Convert an authentication exception into an unauthenticated response.
149 * @param \Illuminate\Http\Request $request
150 * @param \Illuminate\Auth\AuthenticationException $exception
151 * @return \Illuminate\Http\Response
153 protected function unauthenticated($request, AuthenticationException $exception)
155 if ($request->expectsJson()) {
156 return response()->json(['error' => 'Unauthenticated.'], 401);
159 return redirect()->guest('login');
163 * Convert a validation exception into a JSON response.
165 * @param \Illuminate\Http\Request $request
166 * @param \Illuminate\Validation\ValidationException $exception
167 * @return \Illuminate\Http\JsonResponse
169 protected function invalidJson($request, ValidationException $exception)
171 return response()->json($exception->errors(), $exception->status);