From c8b662f1cabb5dadc967ebef2db676de17df9695 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 27 May 2010 22:32:18 +0200
Subject: [PATCH] Add annotations to loadJSArray

This allows, for example, the RAPI backend to detail which information
(instance or node data) fails to parse.
---
 Ganeti/HTools/Rapi.hs  | 6 ++++--
 Ganeti/HTools/Utils.hs | 7 +++++--
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/Ganeti/HTools/Rapi.hs b/Ganeti/HTools/Rapi.hs
index 42d4afe24..183291cd2 100644
--- a/Ganeti/HTools/Rapi.hs
+++ b/Ganeti/HTools/Rapi.hs
@@ -65,11 +65,13 @@ getInstances :: NameAssoc
              -> String
              -> Result [(String, Instance.Instance)]
 getInstances ktn body =
-    loadJSArray body >>= mapM (parseInstance ktn . fromJSObject)
+    loadJSArray "Parsing instance data" body >>=
+    mapM (parseInstance ktn . fromJSObject)
 
 -- | Parse a node list in JSON format.
 getNodes :: String -> Result [(String, Node.Node)]
-getNodes body = loadJSArray body >>= mapM (parseNode . fromJSObject)
+getNodes body = loadJSArray "Parsing node data" body >>=
+                mapM (parseNode . fromJSObject)
 
 -- | Construct an instance from a JSON object.
 parseInstance :: [(String, Ndx)]
diff --git a/Ganeti/HTools/Utils.hs b/Ganeti/HTools/Utils.hs
index 736fcd85d..cf69dae3e 100644
--- a/Ganeti/HTools/Utils.hs
+++ b/Ganeti/HTools/Utils.hs
@@ -112,8 +112,11 @@ readEitherString v =
       _ -> fail "Wrong JSON type"
 
 -- | Converts a JSON message into an array of JSON objects.
-loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
-loadJSArray = fromJResult . J.decodeStrict
+loadJSArray :: (Monad m)
+               => String -- ^ Operation description (for error reporting)
+               -> String -- ^ Input message
+               -> m [J.JSObject J.JSValue]
+loadJSArray s = annotateJResult s . J.decodeStrict
 
 -- | Reads a the value of a key in a JSON object.
 fromObj :: (J.JSON a, Monad m) => String -> [(String, J.JSValue)] -> m a
-- 
GitLab