From 0baf48b4d3b0a56691a9c9b8ed6853465acea070 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 20 Sep 2012 11:50:52 -0400
Subject: [PATCH] Set 404 status when users access missing records.

---
 module/VuFind/src/VuFind/Bootstrap.php | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php
index 447c2cd0fd4..078c38f8a6e 100644
--- a/module/VuFind/src/VuFind/Bootstrap.php
+++ b/module/VuFind/src/VuFind/Bootstrap.php
@@ -309,6 +309,31 @@ class Bootstrap
         $this->events->attach('dispatch', $callback, 10000);
     }
 
+    /**
+     * Set up custom 404 status based on exception type.
+     *
+     * @return void
+     */
+    protected function initExceptionBased404s()
+    {
+        $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);
+                }
+            }
+        };
+        $this->events->attach('dispatch.error', $callback);
+    }
+
     /**
      * Set up logging.
      *
-- 
GitLab