X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/1c43602f4bed60a84f47735ca8bc4a399018e013..refs/pull/5280/head:/app/Exceptions/NotifyException.php diff --git a/app/Exceptions/NotifyException.php b/app/Exceptions/NotifyException.php index ef9a44101..b62b8fde6 100644 --- a/app/Exceptions/NotifyException.php +++ b/app/Exceptions/NotifyException.php @@ -4,31 +4,53 @@ namespace BookStack\Exceptions; use Exception; use Illuminate\Contracts\Support\Responsable; +use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; -class NotifyException extends Exception implements Responsable +class NotifyException extends Exception implements Responsable, HttpExceptionInterface { public $message; - public $redirectLocation; + public string $redirectLocation; + protected int $status; - /** - * NotifyException constructor. - */ - public function __construct(string $message, string $redirectLocation = '/') + public function __construct(string $message, string $redirectLocation = '/', int $status = 500) { $this->message = $message; $this->redirectLocation = $redirectLocation; + $this->status = $status; + parent::__construct(); } + /** + * Get the desired HTTP status code for this exception. + */ + public function getStatusCode(): int + { + return $this->status; + } + + /** + * Get the desired HTTP headers for this exception. + */ + public function getHeaders(): array + { + return []; + } + /** * Send the response for this type of exception. * - * @inheritdoc + * {@inheritdoc} */ public function toResponse($request) { $message = $this->getMessage(); + // Front-end JSON handling. API-side handling managed via handler. + if ($request->wantsJson()) { + return response()->json(['error' => $message], $this->getStatusCode()); + } + if (!empty($message)) { session()->flash('error', $message); }