]> BookStack Code Mirror - bookstack/blob - app/Exceptions/NotifyException.php
Merge branch 'tinymce' into development
[bookstack] / app / Exceptions / NotifyException.php
1 <?php
2
3 namespace BookStack\Exceptions;
4
5 use Exception;
6 use Illuminate\Contracts\Support\Responsable;
7
8 class NotifyException extends Exception implements Responsable
9 {
10     public $message;
11     public $redirectLocation;
12     protected $status;
13
14     /**
15      * NotifyException constructor.
16      */
17     public function __construct(string $message, string $redirectLocation = '/', int $status = 500)
18     {
19         $this->message = $message;
20         $this->redirectLocation = $redirectLocation;
21         $this->status = $status;
22         parent::__construct();
23     }
24
25     /**
26      * Get the desired status code for this exception.
27      */
28     public function getStatus(): int
29     {
30         return $this->status;
31     }
32
33     /**
34      * Send the response for this type of exception.
35      *
36      * {@inheritdoc}
37      */
38     public function toResponse($request)
39     {
40         $message = $this->getMessage();
41
42         // Front-end JSON handling. API-side handling managed via handler.
43         if ($request->wantsJson()) {
44             return response()->json(['error' => $message], 403);
45         }
46
47         if (!empty($message)) {
48             session()->flash('error', $message);
49         }
50
51         return redirect($this->redirectLocation);
52     }
53 }