]> BookStack Code Mirror - bookstack/commitdiff
Moved NotifyException render work from handler to exception
authorDan Brown <redacted>
Sat, 8 May 2021 18:00:09 +0000 (19:00 +0100)
committerDan Brown <redacted>
Sat, 8 May 2021 18:00:09 +0000 (19:00 +0100)
As continued from last commit.

app/Exceptions/Handler.php
app/Exceptions/NotifyException.php
app/Exceptions/PrettyException.php
app/Http/Controllers/PageController.php

index 8d3a743faf939bd18b101c8e982a0d30afc4ad4e..a06c0fdbc103821792a62c86625354096d05a481 100644 (file)
@@ -9,7 +9,6 @@ use Illuminate\Http\JsonResponse;
 use Illuminate\Http\Request;
 use Illuminate\Validation\ValidationException;
 use Symfony\Component\HttpKernel\Exception\HttpException;
-use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 
 class Handler extends ExceptionHandler
 {
@@ -58,16 +57,6 @@ class Handler extends ExceptionHandler
             return $this->renderApiException($e);
         }
 
-        // Handle notify exceptions which will redirect to the
-        // specified location then show a notification message.
-        if ($this->isExceptionType($e, NotifyException::class)) {
-            $message = $this->getOriginalMessage($e);
-            if (!empty($message)) {
-                session()->flash('error', $message);
-            }
-            return redirect($e->redirectLocation);
-        }
-
         return parent::render($request, $e);
     }
 
@@ -106,30 +95,6 @@ class Handler extends ExceptionHandler
         return new JsonResponse($responseData, $code, $headers);
     }
 
-    /**
-     * Check the exception chain to compare against the original exception type.
-     */
-    protected function isExceptionType(Exception $e, string $type): bool
-    {
-        do {
-            if (is_a($e, $type)) {
-                return true;
-            }
-        } while ($e = $e->getPrevious());
-        return false;
-    }
-
-    /**
-     * Get original exception message.
-     */
-    protected function getOriginalMessage(Exception $e): string
-    {
-        do {
-            $message = $e->getMessage();
-        } while ($e = $e->getPrevious());
-        return $message;
-    }
-
     /**
      * Convert an authentication exception into an unauthenticated response.
      *
index 4f810596099cf2132093677b571ce40bf44a5aa8..efca625708b75ef2509d4f9790664c8ab3c18d24 100644 (file)
@@ -1,8 +1,10 @@
 <?php namespace BookStack\Exceptions;
 
-class NotifyException extends \Exception
-{
+use Exception;
+use Illuminate\Contracts\Support\Responsable;
 
+class NotifyException extends Exception implements Responsable
+{
     public $message;
     public $redirectLocation;
 
@@ -15,4 +17,19 @@ class NotifyException extends \Exception
         $this->redirectLocation = $redirectLocation;
         parent::__construct();
     }
+
+    /**
+     * Send the response for this type of exception.
+     * @inheritdoc
+     */
+    public function toResponse($request)
+    {
+        $message = $this->getMessage();
+
+        if (!empty($message)) {
+            session()->flash('error', $message);
+        }
+
+        return redirect($this->redirectLocation);
+    }
 }
index af60c3d06c93c2919275b1c9c11655a278a780cd..8ed135de7c11f0e3c0b89a3845e4dc4d77cfca1c 100644 (file)
@@ -2,7 +2,6 @@
 
 use Exception;
 use Illuminate\Contracts\Support\Responsable;
-use Illuminate\Http\Request;
 
 class PrettyException extends Exception implements Responsable
 {
@@ -18,7 +17,7 @@ class PrettyException extends Exception implements Responsable
 
     /**
      * Render a response for when this exception occurs.
-     * @param Request $request
+     * @inheritdoc
      */
     public function toResponse($request)
     {
index 7d8e54382961006db647b5f1b1b4fe9982337a2d..30d33ad4874eca60b734c542882ebab982381939 100644 (file)
@@ -7,7 +7,6 @@ use BookStack\Entities\Models\Page;
 use BookStack\Entities\Repos\PageRepo;
 use BookStack\Entities\Tools\PermissionsUpdater;
 use BookStack\Exceptions\NotFoundException;
-use BookStack\Exceptions\NotifyException;
 use BookStack\Exceptions\PermissionsException;
 use Exception;
 use Illuminate\Http\Request;
@@ -295,7 +294,6 @@ class PageController extends Controller
      * Remove the specified page from storage.
      * @throws NotFoundException
      * @throws Throwable
-     * @throws NotifyException
      */
     public function destroy(string $bookSlug, string $pageSlug)
     {
@@ -311,7 +309,6 @@ class PageController extends Controller
     /**
      * Remove the specified draft page from storage.
      * @throws NotFoundException
-     * @throws NotifyException
      * @throws Throwable
      */
     public function destroyDraft(string $bookSlug, int $pageId)