3 namespace BookStack\Exceptions;
6 use Illuminate\Contracts\Support\Responsable;
7 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
9 class NotifyException extends Exception implements Responsable, HttpExceptionInterface
12 public $redirectLocation;
18 protected array $headers = [];
20 public function __construct(string $message, string $redirectLocation = '/', int $status = 500)
22 $this->message = $message;
23 $this->redirectLocation = $redirectLocation;
24 $this->status = $status;
26 if ($status >= 300 && $status < 400) {
27 // add redirect header only when a matching HTTP status is given
28 $this->headers = ['location' => $redirectLocation];
31 parent::__construct();
35 * Get the desired HTTP status code for this exception.
39 public function getStatusCode(): int
45 * Get the desired HTTP headers for this exception.
49 public function getHeaders(): array
51 return $this->headers;
55 * @param array<mixed> $headers
57 public function setHeaders(array $headers): void
59 $this->headers = $headers;
63 * Send the response for this type of exception.
67 public function toResponse($request)
69 $message = $this->getMessage();
71 // Front-end JSON handling. API-side handling managed via handler.
72 if ($request->wantsJson()) {
73 return response()->json(['error' => $message], $this->getStatusCode());
76 if (!empty($message)) {
77 session()->flash('error', $message);
80 return redirect($this->redirectLocation);