]> BookStack Code Mirror - bookstack/blob - app/Exceptions/PrettyException.php
Refactor notify exception to clean up api exception handling
[bookstack] / app / Exceptions / PrettyException.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 PrettyException extends Exception implements Responsable, HttpExceptionInterface
10 {
11     /**
12      * @var ?string
13      */
14     protected $subtitle = null;
15
16     /**
17      * @var ?string
18      */
19     protected $details = null;
20
21     /**
22      * @var array
23      */
24     protected $headers = [];
25
26     /**
27      * Render a response for when this exception occurs.
28      *
29      * {@inheritdoc}
30      */
31     public function toResponse($request)
32     {
33         $code = $this->getStatusCode();
34
35         return response()->view('errors.' . $code, [
36             'message'  => $this->getMessage(),
37             'subtitle' => $this->subtitle,
38             'details'  => $this->details,
39         ], $code);
40     }
41
42     public function setSubtitle(string $subtitle): self
43     {
44         $this->subtitle = $subtitle;
45
46         return $this;
47     }
48
49     public function setDetails(string $details): self
50     {
51         $this->details = $details;
52
53         return $this;
54     }
55
56     /**
57      * Get the desired HTTP status code for this exception.
58      */
59     public function getStatusCode(): int
60     {
61         return ($this->getCode() === 0) ? 500 : $this->getCode();
62     }
63
64     /**
65      * Get the desired HTTP headers for this exception.
66      * @return array<mixed>
67      */
68     public function getHeaders(): array
69     {
70         return $this->headers;
71     }
72
73     /**
74      * Set the desired HTTP headers for this exception.
75      * @param array<mixed> $headers
76      */
77     public function setHeaders(array $headers): void
78     {
79         $this->headers = $headers;
80     }
81 }