3 namespace BookStack\Exceptions;
6 use Illuminate\Auth\AuthenticationException;
7 use Illuminate\Database\Eloquent\ModelNotFoundException;
8 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
9 use Illuminate\Http\Exceptions\PostTooLargeException;
10 use Illuminate\Http\JsonResponse;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
13 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
16 class Handler extends ExceptionHandler
19 * A list of the exception types that are not reported.
21 * @var array<int, class-string<\Throwable>>
23 protected $dontReport = [
24 NotFoundException::class,
25 StoppedAuthenticationException::class,
29 * A list of the inputs that are never flashed to the session on validation exceptions.
31 * @var array<int, string>
33 protected $dontFlash = [
36 'password_confirmation',
40 * Report or log an exception.
42 * @param \Throwable $exception
48 public function report(Throwable $exception)
50 parent::report($exception);
54 * Render an exception into an HTTP response.
56 * @param \Illuminate\Http\Request $request
59 * @return \Illuminate\Http\Response
61 public function render($request, Throwable $e)
63 if ($e instanceof PostTooLargeException) {
64 $e = new NotifyException(trans('errors.server_post_limit'), '/', 413);
67 if ($this->isApiRequest($request)) {
68 return $this->renderApiException($e);
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 str_starts_with($request->path(), 'api/');
83 * Render an exception when the API is in use.
85 protected function renderApiException(Throwable $e): JsonResponse
90 if ($e instanceof HttpExceptionInterface) {
91 $code = $e->getStatusCode();
92 $headers = $e->getHeaders();
95 if ($e instanceof ModelNotFoundException) {
101 'message' => $e->getMessage(),
105 if ($e instanceof ValidationException) {
106 $responseData['error']['message'] = 'The given data was invalid.';
107 $responseData['error']['validation'] = $e->errors();
111 $responseData['error']['code'] = $code;
113 return new JsonResponse($responseData, $code, $headers);
117 * Convert an authentication exception into an unauthenticated response.
119 * @param \Illuminate\Http\Request $request
120 * @param \Illuminate\Auth\AuthenticationException $exception
122 * @return \Illuminate\Http\Response
124 protected function unauthenticated($request, AuthenticationException $exception)
126 if ($request->expectsJson()) {
127 return response()->json(['error' => 'Unauthenticated.'], 401);
130 return redirect()->guest('login');
134 * Convert a validation exception into a JSON response.
136 * @param \Illuminate\Http\Request $request
137 * @param \Illuminate\Validation\ValidationException $exception
139 * @return \Illuminate\Http\JsonResponse
141 protected function invalidJson($request, ValidationException $exception)
143 return response()->json($exception->errors(), $exception->status);