From c12a68e23af460d7f1f20620610b1fb162035322 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Sun, 26 Aug 2012 18:20:08 +0200
Subject: [PATCH] Improve error reporting in our JSON conversions

Reporting things such as the following in our error messages
(indentation added by me, not originally present, so it's even worse):

  JSArray [JSArray [JSRational False (1 % 1),JSString
                    (JSONString {fromJSString = "a"})],
           JSArray [JSRational False (2 % 1),
                    JSString (JSONString {fromJSString = "b"})]]

Is not really nice. This patch adds more 'pp_value' conversions so
that the above becomes:

  [[1, "a"], [2, "b"]]

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Agata Murawska <agatamurawska@google.com>
---
 htools/Ganeti/HTools/JSON.hs | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/htools/Ganeti/HTools/JSON.hs b/htools/Ganeti/HTools/JSON.hs
index 2667f80a3..3e2b0dc9b 100644
--- a/htools/Ganeti/HTools/JSON.hs
+++ b/htools/Ganeti/HTools/JSON.hs
@@ -46,6 +46,7 @@ import qualified Data.Map as Map
 import Text.Printf (printf)
 
 import qualified Text.JSON as J
+import Text.JSON.Pretty (pp_value)
 
 import Ganeti.BasicTypes
 
@@ -120,7 +121,7 @@ fromKeyValue k val =
 fromJVal :: (Monad m, J.JSON a) => J.JSValue -> m a
 fromJVal v =
   case J.readJSON v of
-    J.Error s -> fail ("Cannot convert value '" ++ show v ++
+    J.Error s -> fail ("Cannot convert value '" ++ show (pp_value v) ++
                        "', error: " ++ s)
     J.Ok x -> return x
 
@@ -145,7 +146,8 @@ tryFromObj t o = annotateResult t . fromObj o
 -- | Ensure a given JSValue is actually a JSArray.
 toArray :: (Monad m) => J.JSValue -> m [J.JSValue]
 toArray (J.JSArray arr) = return arr
-toArray o = fail $ "Invalid input, expected array but got " ++ show o
+toArray o =
+  fail $ "Invalid input, expected array but got " ++ show (pp_value o)
 
 -- * Container type (special type for JSON serialisation)
 
@@ -170,4 +172,4 @@ instance (J.JSON a) => J.JSON (Container a) where
   showJSON = showContainer
   readJSON (J.JSObject o) = readContainer o
   readJSON v = fail $ "Failed to load container, expected object but got "
-               ++ show v
+               ++ show (pp_value v)
-- 
GitLab