]> BookStack Code Mirror - bookstack/blobdiff - app/Exceptions/NotifyException.php
Added more complexity in an attempt to make ldap host failover fit
[bookstack] / app / Exceptions / NotifyException.php
index efca625708b75ef2509d4f9790664c8ab3c18d24..307916db7a7e7e3fc5635fd9bf76085854b7439d 100644 (file)
@@ -1,4 +1,6 @@
-<?php namespace BookStack\Exceptions;
+<?php
+
+namespace BookStack\Exceptions;
 
 use Exception;
 use Illuminate\Contracts\Support\Responsable;
@@ -7,25 +9,38 @@ class NotifyException extends Exception implements Responsable
 {
     public $message;
     public $redirectLocation;
+    protected $status;
 
-    /**
-     * NotifyException constructor.
-     */
-    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 status code for this exception.
+     */
+    public function getStatus(): int
+    {
+        return $this->status;
+    }
+
     /**
      * Send the response for this type of exception.
-     * @inheritdoc
+     *
+     * {@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], 403);
+        }
+
         if (!empty($message)) {
             session()->flash('error', $message);
         }