]> BookStack Code Mirror - bookstack/commitdiff
Refactor exception handling by using interface
authorThomas Kuschan <redacted>
Tue, 13 Jun 2023 16:40:37 +0000 (18:40 +0200)
committerThomas Kuschan <redacted>
Tue, 13 Jun 2023 16:52:02 +0000 (18:52 +0200)
app/Exceptions/Handler.php
app/Exceptions/PrettyException.php

index 00122c15ad4b54fb626ec5487d02759dff499b17..02da3d5debf3823d19e9ebae687819ed663addd7 100644 (file)
@@ -9,7 +9,7 @@ use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
 use Illuminate\Http\JsonResponse;
 use Illuminate\Http\Request;
 use Illuminate\Validation\ValidationException;
-use Symfony\Component\HttpKernel\Exception\HttpException;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
 use Throwable;
 
 class Handler extends ExceptionHandler
@@ -79,10 +79,10 @@ class Handler extends ExceptionHandler
      */
     protected function renderApiException(Throwable $e): JsonResponse
     {
-        $code = $e->getCode() === 0 ? 500 : $e->getCode();
+        $code = 500;
         $headers = [];
 
-        if ($e instanceof HttpException) {
+        if ($e instanceof HttpExceptionInterface) {
             $code = $e->getStatusCode();
             $headers = $e->getHeaders();
         }
index f446442d058dbcb7e383879208c30ea239970f5c..d0aca59225a4fc09c24fdfa344dcfe8c2569057f 100644 (file)
@@ -4,8 +4,9 @@ namespace BookStack\Exceptions;
 
 use Exception;
 use Illuminate\Contracts\Support\Responsable;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
 
-class PrettyException extends Exception implements Responsable
+class PrettyException extends Exception implements Responsable, HttpExceptionInterface
 {
     /**
      * @var ?string
@@ -17,6 +18,11 @@ class PrettyException extends Exception implements Responsable
      */
     protected $details = null;
 
+    /**
+     * @var array
+     */
+    protected $headers = [];
+
     /**
      * Render a response for when this exception occurs.
      *
@@ -24,7 +30,7 @@ class PrettyException extends Exception implements Responsable
      */
     public function toResponse($request)
     {
-        $code = ($this->getCode() === 0) ? 500 : $this->getCode();
+        $code = $this->getStatusCode();
 
         return response()->view('errors.' . $code, [
             'message'  => $this->getMessage(),
@@ -46,4 +52,30 @@ class PrettyException extends Exception implements Responsable
 
         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;
+    }
 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.