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\Http\Response;
13 use Illuminate\Validation\ValidationException;
14 use Symfony\Component\ErrorHandler\Error\FatalError;
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 \Illuminate\Http\Request $request
70 * @return \Illuminate\Http\Response
72 public function render($request, Throwable $e)
74 if ($e instanceof FatalError && str_contains($e->getMessage(), 'bytes exhausted (tried to allocate') && $this->onOutOfMemory) {
75 $response = call_user_func($this->onOutOfMemory);
81 if ($e instanceof PostTooLargeException) {
82 $e = new NotifyException(trans('errors.server_post_limit'), '/', 413);
85 if ($this->isApiRequest($request)) {
86 return $this->renderApiException($e);
89 return parent::render($request, $e);
93 * Provide a function to be called when an out of memory event occurs.
94 * If the callable returns a response, this response will be returned
95 * to the request upon error.
97 public function prepareForOutOfMemory(callable $onOutOfMemory)
99 $this->onOutOfMemory = $onOutOfMemory;
103 * Forget the current out of memory handler, if existing.
105 public function forgetOutOfMemoryHandler()
107 $this->onOutOfMemory = null;
111 * Check if the given request is an API request.
113 protected function isApiRequest(Request $request): bool
115 return str_starts_with($request->path(), 'api/');
119 * Render an exception when the API is in use.
121 protected function renderApiException(Throwable $e): JsonResponse
126 if ($e instanceof HttpExceptionInterface) {
127 $code = $e->getStatusCode();
128 $headers = $e->getHeaders();
131 if ($e instanceof ModelNotFoundException) {
137 'message' => $e->getMessage(),
141 if ($e instanceof ValidationException) {
142 $responseData['error']['message'] = 'The given data was invalid.';
143 $responseData['error']['validation'] = $e->errors();
147 $responseData['error']['code'] = $code;
149 return new JsonResponse($responseData, $code, $headers);
153 * Convert an authentication exception into an unauthenticated response.
155 * @param \Illuminate\Http\Request $request
156 * @param \Illuminate\Auth\AuthenticationException $exception
158 * @return \Illuminate\Http\Response
160 protected function unauthenticated($request, AuthenticationException $exception)
162 if ($request->expectsJson()) {
163 return response()->json(['error' => 'Unauthenticated.'], 401);
166 return redirect()->guest('login');
170 * Convert a validation exception into a JSON response.
172 * @param \Illuminate\Http\Request $request
173 * @param \Illuminate\Validation\ValidationException $exception
175 * @return \Illuminate\Http\JsonResponse
177 protected function invalidJson($request, ValidationException $exception)
179 return response()->json($exception->errors(), $exception->status);