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