Skip to content

NOW MANAGED ON CODEBERG

Moved NotifyException render work from handler to exception

Dan Brown9e7bcacf8c56
diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php
index 8d3a743fa..a06c0fdbc 100644
--- a/app/Exceptions/Handler.php
+++ b/app/Exceptions/Handler.php
@@ -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.
*
diff --git a/app/Exceptions/NotifyException.php b/app/Exceptions/NotifyException.php
index 4f8105960..efca62570 100644
--- a/app/Exceptions/NotifyException.php
+++ b/app/Exceptions/NotifyException.php
@@ -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);
+ }
}
diff --git a/app/Exceptions/PrettyException.php b/app/Exceptions/PrettyException.php
index af60c3d06..8ed135de7 100644
--- a/app/Exceptions/PrettyException.php
+++ b/app/Exceptions/PrettyException.php
@@ -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)
{
diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php
index 7d8e54382..30d33ad48 100644
--- a/app/Http/Controllers/PageController.php
+++ b/app/Http/Controllers/PageController.php
@@ -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)