From bdd7c62da4e99707415ffce6fe8f47a0a3bde383 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 8 Jun 2016 07:45:24 -0400
Subject: [PATCH] More generic mechanism for linking exceptions to HTTP
 statuses.

---
 module/VuFind/src/VuFind/Bootstrapper.php     | 22 ++++-----
 .../VuFind/src/VuFind/Exception/Forbidden.php | 11 ++++-
 .../VuFind/Exception/HttpStatusInterface.php  | 49 +++++++++++++++++++
 .../src/VuFind/Exception/RecordMissing.php    | 11 ++++-
 4 files changed, 78 insertions(+), 15 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/Exception/HttpStatusInterface.php

diff --git a/module/VuFind/src/VuFind/Bootstrapper.php b/module/VuFind/src/VuFind/Bootstrapper.php
index 1d139917e69..025de89ab92 100644
--- a/module/VuFind/src/VuFind/Bootstrapper.php
+++ b/module/VuFind/src/VuFind/Bootstrapper.php
@@ -391,30 +391,26 @@ class Bootstrapper
     }
 
     /**
-     * Set up custom 404 status based on exception type.
+     * Set up custom HTTP status based on exception information.
      *
      * @return void
      */
-    protected function initExceptionBased404s()
+    protected function initExceptionBasedHttpStatuses()
     {
-        // 404s not needed in console mode:
+        // HTTP statuses not needed in console mode:
         if (Console::isConsole()) {
             return;
         }
 
         $callback = function ($e) {
             $exception = $e->getParam('exception');
-            if (is_object($exception)) {
-                if ($exception instanceof \VuFind\Exception\RecordMissing) {
-                    // TODO: it might be better to solve this problem by using a
-                    // custom RouteNotFoundStrategy.
-                    $response = $e->getResponse();
-                    if (!$response) {
-                        $response = new HttpResponse();
-                        $e->setResponse($response);
-                    }
-                    $response->setStatusCode(404);
+            if ($exception instanceof \VuFind\Exception\HttpStatusInterface) {
+                $response = $e->getResponse();
+                if (!$response) {
+                    $response = new HttpResponse();
+                    $e->setResponse($response);
                 }
+                $response->setStatusCode($exception->getHttpStatus());
             }
         };
         $this->events->attach('dispatch.error', $callback);
diff --git a/module/VuFind/src/VuFind/Exception/Forbidden.php b/module/VuFind/src/VuFind/Exception/Forbidden.php
index fe1c96f89c5..69daa832166 100644
--- a/module/VuFind/src/VuFind/Exception/Forbidden.php
+++ b/module/VuFind/src/VuFind/Exception/Forbidden.php
@@ -36,6 +36,15 @@ namespace VuFind\Exception;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     https://vufind.org/wiki/development Wiki
  */
-class Forbidden extends \Exception
+class Forbidden extends \Exception implements HttpStatusInterface
 {
+    /**
+     * Get HTTP status associated with this exception.
+     *
+     * @return int
+     */
+    public function getHttpStatus()
+    {
+        return 403;
+    }
 }
diff --git a/module/VuFind/src/VuFind/Exception/HttpStatusInterface.php b/module/VuFind/src/VuFind/Exception/HttpStatusInterface.php
new file mode 100644
index 00000000000..2404baf1e7e
--- /dev/null
+++ b/module/VuFind/src/VuFind/Exception/HttpStatusInterface.php
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Interface for exceptions that should trigger specific HTTP status codes
+ * when unhandled.
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2011.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind
+ * @package  Exceptions
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+namespace VuFind\Exception;
+
+/**
+ * Interface for exceptions that should trigger specific HTTP status codes
+ * when unhandled.
+ *
+ * @category VuFind
+ * @package  Exceptions
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+interface HttpStatusInterface
+{
+    /**
+     * Get HTTP status associated with this exception.
+     *
+     * @return int
+     */
+    public function getHttpStatus();
+}
diff --git a/module/VuFind/src/VuFind/Exception/RecordMissing.php b/module/VuFind/src/VuFind/Exception/RecordMissing.php
index 89b0d9f36da..552d77506f2 100644
--- a/module/VuFind/src/VuFind/Exception/RecordMissing.php
+++ b/module/VuFind/src/VuFind/Exception/RecordMissing.php
@@ -36,6 +36,15 @@ namespace VuFind\Exception;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     https://vufind.org/wiki/development Wiki
  */
-class RecordMissing extends \Exception
+class RecordMissing extends \Exception implements HttpStatusInterface
 {
+    /**
+     * Get HTTP status associated with this exception.
+     *
+     * @return int
+     */
+    public function getHttpStatus()
+    {
+        return 404;
+    }
 }
-- 
GitLab