]> BookStack Code Mirror - bookstack/blobdiff - app/Exceptions/NotifyException.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Exceptions / NotifyException.php
index 78ffde05c07e28655ad2cb25ca5a4f04553d2dfc..b62b8fde64673a767b28996e79ae97b6e4ebd4c7 100644 (file)
@@ -1,20 +1,60 @@
-<?php namespace BookStack\Exceptions;
+<?php
 
-class NotifyException extends \Exception
-{
+namespace BookStack\Exceptions;
+
+use Exception;
+use Illuminate\Contracts\Support\Responsable;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
 
+class NotifyException extends Exception implements Responsable, HttpExceptionInterface
+{
     public $message;
-    public $redirectLocation;
+    public string $redirectLocation;
+    protected int $status;
 
-    /**
-     * NotifyException constructor.
-     * @param string $message
-     * @param string    $redirectLocation
-     */
-    public function __construct(string $message, string $redirectLocation = "/")
+    public function __construct(string $message, string $redirectLocation = '/', int $status = 500)
     {
         $this->message = $message;
         $this->redirectLocation = $redirectLocation;
+        $this->status = $status;
+
         parent::__construct();
     }
+
+    /**
+     * Get the desired HTTP status code for this exception.
+     */
+    public function getStatusCode(): int
+    {
+        return $this->status;
+    }
+
+    /**
+     * Get the desired HTTP headers for this exception.
+     */
+    public function getHeaders(): array
+    {
+        return [];
+    }
+
+    /**
+     * Send the response for this type of exception.
+     *
+     * {@inheritdoc}
+     */
+    public function toResponse($request)
+    {
+        $message = $this->getMessage();
+
+        // Front-end JSON handling. API-side handling managed via handler.
+        if ($request->wantsJson()) {
+            return response()->json(['error' => $message], $this->getStatusCode());
+        }
+
+        if (!empty($message)) {
+            session()->flash('error', $message);
+        }
+
+        return redirect($this->redirectLocation);
+    }
 }