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 return parent::render($request, $e);
75 * Check if the given request is an API request.
77 protected function isApiRequest(Request $request): bool
79 return strpos($request->path(), 'api/') === 0;
83 * Render an exception when the API is in use.
85 protected function renderApiException(Exception $e): JsonResponse
87 $code = $e->getCode() === 0 ? 500 : $e->getCode();
89 if ($e instanceof HttpException) {
90 $code = $e->getStatusCode();
91 $headers = $e->getHeaders();
96 'message' => $e->getMessage(),
100 if ($e instanceof ValidationException) {
101 $responseData['error']['validation'] = $e->errors();
105 $responseData['error']['code'] = $code;
106 return new JsonResponse($responseData, $code, $headers);
110 * Check the exception chain to compare against the original exception type.
112 protected function isExceptionType(Exception $e, string $type): bool
115 if (is_a($e, $type)) {
118 } while ($e = $e->getPrevious());
123 * Get original exception message.
125 protected function getOriginalMessage(Exception $e): string
128 $message = $e->getMessage();
129 } while ($e = $e->getPrevious());
134 * Convert an authentication exception into an unauthenticated response.
136 * @param \Illuminate\Http\Request $request
137 * @param \Illuminate\Auth\AuthenticationException $exception
138 * @return \Illuminate\Http\Response
140 protected function unauthenticated($request, AuthenticationException $exception)
142 if ($request->expectsJson()) {
143 return response()->json(['error' => 'Unauthenticated.'], 401);
146 return redirect()->guest('login');
150 * Convert a validation exception into a JSON response.
152 * @param \Illuminate\Http\Request $request
153 * @param \Illuminate\Validation\ValidationException $exception
154 * @return \Illuminate\Http\JsonResponse
156 protected function invalidJson($request, ValidationException $exception)
158 return response()->json($exception->errors(), $exception->status);