]> BookStack Code Mirror - bookstack/blob - app/Exceptions/NotifyException.php
Refactored common list handling operations to new class
[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     public function __construct(string $message, string $redirectLocation = '/', int $status = 500)
15     {
16         $this->message = $message;
17         $this->redirectLocation = $redirectLocation;
18         $this->status = $status;
19         parent::__construct();
20     }
21
22     /**
23      * Get the desired status code for this exception.
24      */
25     public function getStatus(): int
26     {
27         return $this->status;
28     }
29
30     /**
31      * Send the response for this type of exception.
32      *
33      * {@inheritdoc}
34      */
35     public function toResponse($request)
36     {
37         $message = $this->getMessage();
38
39         // Front-end JSON handling. API-side handling managed via handler.
40         if ($request->wantsJson()) {
41             return response()->json(['error' => $message], 403);
42         }
43
44         if (!empty($message)) {
45             session()->flash('error', $message);
46         }
47
48         return redirect($this->redirectLocation);
49     }
50 }