Add proper error management to monitoring daemon
authorMichele Tartara <mtartara@google.com>
Wed, 24 Apr 2013 10:20:29 +0000 (10:20 +0000)
committerMichele Tartara <mtartara@google.com>
Tue, 30 Apr 2013 07:57:03 +0000 (09:57 +0200)
Produce a personalized 404 error when the requested resource is not available.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Helga Velroyen <helgav@google.com>

src/Ganeti/Monitoring/Server.hs

index 1f311e1..f31524e 100644 (file)
@@ -135,7 +135,8 @@ reportHandler =
   route
     [ ("all", allReports)
     , (":category/:collector", oneReport)
-    ]
+    ] <|>
+  errorReport
 
 -- | Return the report of all the available collectors.
 allReports :: Snap ()
@@ -154,6 +155,16 @@ catFromName "hypervisor" = BT.Ok $ Just DCHypervisor
 catFromName "default"    = BT.Ok Nothing
 catFromName _            = BT.Bad "No such category"
 
+errorReport :: Snap ()
+errorReport = do
+  modifyResponse $ setResponseStatus 404 "Not found"
+  writeBS "Unable to produce a report for the requested resource"
+
+error404 :: Snap ()
+error404 = do
+  modifyResponse $ setResponseStatus 404 "Not found"
+  writeBS "Resource not found"
+
 -- | Return the report of one collector
 oneReport :: Snap ()
 oneReport = do
@@ -178,7 +189,8 @@ oneReport = do
 monitoringApi :: Snap ()
 monitoringApi =
   ifTop versionQ <|>
-  dir "1" version1Api
+  dir "1" version1Api <|>
+  error404
 
 -- | Main function.
 main :: MainFn CheckResult PrepResult