2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exceptions;
|
2015-09-04 12:16:58 -04:00
|
|
|
|
2021-05-08 14:00:09 -04:00
|
|
|
use Exception;
|
|
|
|
use Illuminate\Contracts\Support\Responsable;
|
2023-06-14 05:07:13 -04:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
2015-09-04 12:16:58 -04:00
|
|
|
|
2023-06-14 05:07:13 -04:00
|
|
|
class NotifyException extends Exception implements Responsable, HttpExceptionInterface
|
2021-05-08 14:00:09 -04:00
|
|
|
{
|
2015-09-04 12:16:58 -04:00
|
|
|
public $message;
|
|
|
|
public $redirectLocation;
|
2022-02-03 19:26:19 -05:00
|
|
|
protected $status;
|
2015-09-04 12:16:58 -04:00
|
|
|
|
2023-06-14 05:07:13 -04:00
|
|
|
/**
|
|
|
|
* @var array<mixed>
|
|
|
|
*/
|
|
|
|
protected array $headers = [];
|
|
|
|
|
2022-02-03 19:26:19 -05:00
|
|
|
public function __construct(string $message, string $redirectLocation = '/', int $status = 500)
|
2015-09-04 12:16:58 -04:00
|
|
|
{
|
|
|
|
$this->message = $message;
|
|
|
|
$this->redirectLocation = $redirectLocation;
|
2022-02-03 19:26:19 -05:00
|
|
|
$this->status = $status;
|
2023-06-14 05:07:13 -04:00
|
|
|
|
|
|
|
if ($status >= 300 && $status < 400) {
|
|
|
|
// add redirect header only when a matching HTTP status is given
|
|
|
|
$this->headers = ['location' => $redirectLocation];
|
|
|
|
}
|
|
|
|
|
2015-09-04 12:16:58 -04:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2021-05-08 14:00:09 -04:00
|
|
|
|
2022-02-03 19:26:19 -05:00
|
|
|
/**
|
2023-06-14 05:07:13 -04:00
|
|
|
* Get the desired HTTP status code for this exception.
|
|
|
|
*
|
|
|
|
* {@inheritdoc}
|
2022-02-03 19:26:19 -05:00
|
|
|
*/
|
2023-06-14 05:07:13 -04:00
|
|
|
public function getStatusCode(): int
|
2022-02-03 19:26:19 -05:00
|
|
|
{
|
|
|
|
return $this->status;
|
|
|
|
}
|
|
|
|
|
2023-06-14 05:07:13 -04:00
|
|
|
/**
|
|
|
|
* Get the desired HTTP headers for this exception.
|
|
|
|
*
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getHeaders(): array
|
|
|
|
{
|
|
|
|
return $this->headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<mixed> $headers
|
|
|
|
*/
|
|
|
|
public function setHeaders(array $headers): void
|
|
|
|
{
|
|
|
|
$this->headers = $headers;
|
|
|
|
}
|
|
|
|
|
2021-05-08 14:00:09 -04:00
|
|
|
/**
|
|
|
|
* Send the response for this type of exception.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-10-26 17:04:18 -04:00
|
|
|
* {@inheritdoc}
|
2021-05-08 14:00:09 -04:00
|
|
|
*/
|
|
|
|
public function toResponse($request)
|
|
|
|
{
|
|
|
|
$message = $this->getMessage();
|
|
|
|
|
2022-02-03 20:02:13 -05:00
|
|
|
// Front-end JSON handling. API-side handling managed via handler.
|
|
|
|
if ($request->wantsJson()) {
|
2023-06-14 05:07:13 -04:00
|
|
|
return response()->json(['error' => $message], $this->getStatusCode());
|
2022-02-03 20:02:13 -05:00
|
|
|
}
|
|
|
|
|
2021-05-08 14:00:09 -04:00
|
|
|
if (!empty($message)) {
|
|
|
|
session()->flash('error', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect($this->redirectLocation);
|
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|