3 namespace BookStack\Exceptions;
5 use Illuminate\Auth\AuthenticationException;
6 use Illuminate\Database\Eloquent\ModelNotFoundException;
7 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8 use Illuminate\Http\Exceptions\PostTooLargeException;
9 use Illuminate\Http\JsonResponse;
10 use Illuminate\Http\Request;
11 use Illuminate\Http\Response;
12 use Illuminate\Validation\ValidationException;
13 use Symfony\Component\ErrorHandler\Error\FatalError;
14 use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
15 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
18 class Handler extends ExceptionHandler
21 * A list of the exception types that are not reported.
23 * @var array<int, class-string<Throwable>>
25 protected $dontReport = [
26 NotFoundException::class,
27 StoppedAuthenticationException::class,
31 * A list of the inputs that are never flashed to the session on validation exceptions.
33 * @var array<int, string>
35 protected $dontFlash = [
38 'password_confirmation',
42 * A function to run upon out of memory.
43 * If it returns a response, that will be provided back to the request
44 * upon an out of memory event.
46 * @var ?callable(): ?Response
48 protected $onOutOfMemory = null;
51 * Report or log an exception.
53 * @param Throwable $exception
59 public function report(Throwable $exception)
61 parent::report($exception);
65 * Render an exception into an HTTP response.
67 * @param Request $request
69 public function render($request, Throwable $e): SymfonyResponse
71 if ($e instanceof FatalError && str_contains($e->getMessage(), 'bytes exhausted (tried to allocate') && $this->onOutOfMemory) {
72 $response = call_user_func($this->onOutOfMemory);
78 if ($e instanceof PostTooLargeException) {
79 $e = new NotifyException(trans('errors.server_post_limit'), '/', 413);
82 if ($this->isApiRequest($request)) {
83 return $this->renderApiException($e);
86 return parent::render($request, $e);
90 * Provide a function to be called when an out of memory event occurs.
91 * If the callable returns a response, this response will be returned
92 * to the request upon error.
94 public function prepareForOutOfMemory(callable $onOutOfMemory): void
96 $this->onOutOfMemory = $onOutOfMemory;
100 * Forget the current out of memory handler, if existing.
102 public function forgetOutOfMemoryHandler(): void
104 $this->onOutOfMemory = null;
108 * Check if the given request is an API request.
110 protected function isApiRequest(Request $request): bool
112 return str_starts_with($request->path(), 'api/');
116 * Render an exception when the API is in use.
118 protected function renderApiException(Throwable $e): JsonResponse
123 if ($e instanceof HttpExceptionInterface) {
124 $code = $e->getStatusCode();
125 $headers = $e->getHeaders();
128 if ($e instanceof ModelNotFoundException) {
134 'message' => $e->getMessage(),
138 if ($e instanceof ValidationException) {
139 $responseData['error']['message'] = 'The given data was invalid.';
140 $responseData['error']['validation'] = $e->errors();
144 $responseData['error']['code'] = $code;
146 return new JsonResponse($responseData, $code, $headers);
150 * Convert an authentication exception into an unauthenticated response.
152 * @param Request $request
154 protected function unauthenticated($request, AuthenticationException $exception): SymfonyResponse
156 if ($request->expectsJson()) {
157 return response()->json(['error' => 'Unauthenticated.'], 401);
160 return redirect()->guest('login');
164 * Convert a validation exception into a JSON response.
166 * @param Request $request
168 protected function invalidJson($request, ValidationException $exception): JsonResponse
170 return response()->json($exception->errors(), $exception->status);