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\ErrorHandler\Error\FatalError;
14 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
17 class Handler extends ExceptionHandler
20 * A list of the exception types that are not reported.
22 * @var array<int, class-string<\Throwable>>
24 protected $dontReport = [
25 NotFoundException::class,
26 StoppedAuthenticationException::class,
30 * A list of the inputs that are never flashed to the session on validation exceptions.
32 * @var array<int, string>
34 protected $dontFlash = [
37 'password_confirmation',
41 * A function to run upon out of memory.
42 * If it returns a response, that will be provided back to the request
43 * upon an out of memory event.
45 * @var ?callable<?\Illuminate\Http\Response>
47 protected $onOutOfMemory = null;
50 * Report or log an exception.
52 * @param \Throwable $exception
58 public function report(Throwable $exception)
60 parent::report($exception);
64 * Render an exception into an HTTP response.
66 * @param \Illuminate\Http\Request $request
69 * @return \Illuminate\Http\Response
71 public function render($request, Throwable $e)
73 if ($e instanceof FatalError && str_contains($e->getMessage(), 'bytes exhausted (tried to allocate') && $this->onOutOfMemory) {
74 $response = call_user_func($this->onOutOfMemory);
80 if ($e instanceof PostTooLargeException) {
81 $e = new NotifyException(trans('errors.server_post_limit'), '/', 413);
84 if ($this->isApiRequest($request)) {
85 return $this->renderApiException($e);
88 return parent::render($request, $e);
92 * Provide a function to be called when an out of memory event occurs.
93 * If the callable returns a response, this response will be returned
94 * to the request upon error.
96 public function prepareForOutOfMemory(callable $onOutOfMemory)
98 $this->onOutOfMemory = $onOutOfMemory;
102 * Forget the current out of memory handler, if existing.
104 public function forgetOutOfMemoryHandler()
106 $this->onOutOfMemory = null;
110 * Check if the given request is an API request.
112 protected function isApiRequest(Request $request): bool
114 return str_starts_with($request->path(), 'api/');
118 * Render an exception when the API is in use.
120 protected function renderApiException(Throwable $e): JsonResponse
125 if ($e instanceof HttpExceptionInterface) {
126 $code = $e->getStatusCode();
127 $headers = $e->getHeaders();
130 if ($e instanceof ModelNotFoundException) {
136 'message' => $e->getMessage(),
140 if ($e instanceof ValidationException) {
141 $responseData['error']['message'] = 'The given data was invalid.';
142 $responseData['error']['validation'] = $e->errors();
146 $responseData['error']['code'] = $code;
148 return new JsonResponse($responseData, $code, $headers);
152 * Convert an authentication exception into an unauthenticated response.
154 * @param \Illuminate\Http\Request $request
155 * @param \Illuminate\Auth\AuthenticationException $exception
157 * @return \Illuminate\Http\Response
159 protected function unauthenticated($request, AuthenticationException $exception)
161 if ($request->expectsJson()) {
162 return response()->json(['error' => 'Unauthenticated.'], 401);
165 return redirect()->guest('login');
169 * Convert a validation exception into a JSON response.
171 * @param \Illuminate\Http\Request $request
172 * @param \Illuminate\Validation\ValidationException $exception
174 * @return \Illuminate\Http\JsonResponse
176 protected function invalidJson($request, ValidationException $exception)
178 return response()->json($exception->errors(), $exception->status);