]> BookStack Code Mirror - bookstack/blobdiff - app/Exceptions/PrettyException.php
Refactor notify exception to clean up api exception handling
[bookstack] / app / Exceptions / PrettyException.php
index af60c3d06c93c2919275b1c9c11655a278a780cd..d0aca59225a4fc09c24fdfa344dcfe8c2569057f 100644 (file)
@@ -1,10 +1,12 @@
-<?php namespace BookStack\Exceptions;
+<?php
+
+namespace BookStack\Exceptions;
 
 use Exception;
 use Illuminate\Contracts\Support\Responsable;
-use Illuminate\Http\Request;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
 
-class PrettyException extends Exception implements Responsable
+class PrettyException extends Exception implements Responsable, HttpExceptionInterface
 {
     /**
      * @var ?string
@@ -16,29 +18,64 @@ class PrettyException extends Exception implements Responsable
      */
     protected $details = null;
 
+    /**
+     * @var array
+     */
+    protected $headers = [];
+
     /**
      * Render a response for when this exception occurs.
-     * @param Request $request
+     *
+     * {@inheritdoc}
      */
     public function toResponse($request)
     {
-        $code = ($this->getCode() === 0) ? 500 : $this->getCode();
+        $code = $this->getStatusCode();
+
         return response()->view('errors.' . $code, [
-            'message' => $this->getMessage(),
+            'message'  => $this->getMessage(),
             'subtitle' => $this->subtitle,
-            'details' => $this->details,
+            'details'  => $this->details,
         ], $code);
     }
 
     public function setSubtitle(string $subtitle): self
     {
         $this->subtitle = $subtitle;
+
         return $this;
     }
 
     public function setDetails(string $details): self
     {
         $this->details = $details;
+
         return $this;
     }
+
+    /**
+     * Get the desired HTTP status code for this exception.
+     */
+    public function getStatusCode(): int
+    {
+        return ($this->getCode() === 0) ? 500 : $this->getCode();
+    }
+
+    /**
+     * Get the desired HTTP headers for this exception.
+     * @return array<mixed>
+     */
+    public function getHeaders(): array
+    {
+        return $this->headers;
+    }
+
+    /**
+     * Set the desired HTTP headers for this exception.
+     * @param array<mixed> $headers
+     */
+    public function setHeaders(array $headers): void
+    {
+        $this->headers = $headers;
+    }
 }