2021-06-26 15:23:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exceptions;
|
2015-09-04 16:16:58 +00:00
|
|
|
|
2021-05-08 18:00:09 +00:00
|
|
|
use Exception;
|
|
|
|
use Illuminate\Contracts\Support\Responsable;
|
2015-09-04 16:16:58 +00:00
|
|
|
|
2021-05-08 18:00:09 +00:00
|
|
|
class NotifyException extends Exception implements Responsable
|
|
|
|
{
|
2015-09-04 16:16:58 +00:00
|
|
|
public $message;
|
|
|
|
public $redirectLocation;
|
2022-02-04 00:26:19 +00:00
|
|
|
protected $status;
|
2015-09-04 16:16:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* NotifyException constructor.
|
|
|
|
*/
|
2022-02-04 00:26:19 +00:00
|
|
|
public function __construct(string $message, string $redirectLocation = '/', int $status = 500)
|
2015-09-04 16:16:58 +00:00
|
|
|
{
|
|
|
|
$this->message = $message;
|
|
|
|
$this->redirectLocation = $redirectLocation;
|
2022-02-04 00:26:19 +00:00
|
|
|
$this->status = $status;
|
2015-09-04 16:16:58 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2021-05-08 18:00:09 +00:00
|
|
|
|
2022-02-04 00:26:19 +00:00
|
|
|
/**
|
|
|
|
* Get the desired status code for this exception.
|
|
|
|
*/
|
|
|
|
public function getStatus(): int
|
|
|
|
{
|
|
|
|
return $this->status;
|
|
|
|
}
|
|
|
|
|
2021-05-08 18:00:09 +00:00
|
|
|
/**
|
|
|
|
* Send the response for this type of exception.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2021-10-26 21:04:18 +00:00
|
|
|
* {@inheritdoc}
|
2021-05-08 18:00:09 +00:00
|
|
|
*/
|
|
|
|
public function toResponse($request)
|
|
|
|
{
|
|
|
|
$message = $this->getMessage();
|
|
|
|
|
2022-02-04 01:02:13 +00:00
|
|
|
// Front-end JSON handling. API-side handling managed via handler.
|
|
|
|
if ($request->wantsJson()) {
|
|
|
|
return response()->json(['error' => $message], 403);
|
|
|
|
}
|
|
|
|
|
2021-05-08 18:00:09 +00:00
|
|
|
if (!empty($message)) {
|
|
|
|
session()->flash('error', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect($this->redirectLocation);
|
|
|
|
}
|
2018-01-28 16:58:52 +00:00
|
|
|
}
|