]> BookStack Code Mirror - bookstack/commitdiff
Refactor notify exception to clean up api exception handling 4291/head
authorThomas Kuschan <redacted>
Wed, 14 Jun 2023 09:07:13 +0000 (11:07 +0200)
committerThomas Kuschan <redacted>
Wed, 14 Jun 2023 09:08:20 +0000 (11:08 +0200)
app/Exceptions/Handler.php
app/Exceptions/NotifyException.php

index 02da3d5debf3823d19e9ebae687819ed663addd7..36bdf845d64d8905e930ab04385ff184f195a267 100644 (file)
@@ -103,10 +103,6 @@ class Handler extends ExceptionHandler
             $code = $e->status;
         }
 
             $code = $e->status;
         }
 
-        if (method_exists($e, 'getStatus')) {
-            $code = $e->getStatus();
-        }
-
         $responseData['error']['code'] = $code;
 
         return new JsonResponse($responseData, $code, $headers);
         $responseData['error']['code'] = $code;
 
         return new JsonResponse($responseData, $code, $headers);
index 307916db7a7e7e3fc5635fd9bf76085854b7439d..67ef27a75a5f9167eb25b9f13a06545446911aff 100644 (file)
@@ -4,29 +4,61 @@ namespace BookStack\Exceptions;
 
 use Exception;
 use Illuminate\Contracts\Support\Responsable;
 
 use Exception;
 use Illuminate\Contracts\Support\Responsable;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
 
 
-class NotifyException extends Exception implements Responsable
+class NotifyException extends Exception implements Responsable, HttpExceptionInterface
 {
     public $message;
     public $redirectLocation;
     protected $status;
 
 {
     public $message;
     public $redirectLocation;
     protected $status;
 
+    /**
+     * @var array<mixed>
+     */
+    protected array $headers = [];
+
     public function __construct(string $message, string $redirectLocation = '/', int $status = 500)
     {
         $this->message = $message;
         $this->redirectLocation = $redirectLocation;
         $this->status = $status;
     public function __construct(string $message, string $redirectLocation = '/', int $status = 500)
     {
         $this->message = $message;
         $this->redirectLocation = $redirectLocation;
         $this->status = $status;
+
+        if ($status >= 300 && $status < 400) {
+            // add redirect header only when a matching HTTP status is given
+            $this->headers = ['location' => $redirectLocation];
+        }
+
         parent::__construct();
     }
 
     /**
         parent::__construct();
     }
 
     /**
-     * Get the desired status code for this exception.
+     * Get the desired HTTP status code for this exception.
+     *
+     * {@inheritdoc}
      */
      */
-    public function getStatus(): int
+    public function getStatusCode(): int
     {
         return $this->status;
     }
 
     {
         return $this->status;
     }
 
+    /**
+     * Get the desired HTTP headers for this exception.
+     *
+     * {@inheritdoc}
+     */
+    public function getHeaders(): array
+    {
+        return $this->headers;
+    }
+
+    /**
+     * @param array<mixed> $headers
+     */
+    public function setHeaders(array $headers): void
+    {
+        $this->headers = $headers;
+    }
+
     /**
      * Send the response for this type of exception.
      *
     /**
      * Send the response for this type of exception.
      *
@@ -38,7 +70,7 @@ class NotifyException extends Exception implements Responsable
 
         // Front-end JSON handling. API-side handling managed via handler.
         if ($request->wantsJson()) {
 
         // Front-end JSON handling. API-side handling managed via handler.
         if ($request->wantsJson()) {
-            return response()->json(['error' => $message], 403);
+            return response()->json(['error' => $message], $this->getStatusCode());
         }
 
         if (!empty($message)) {
         }
 
         if (!empty($message)) {