Commit c96d44df authored by Iustin Pop's avatar Iustin Pop
Browse files

Replace fromJResult with annotateJResult

This patch removes all old uses of fromJResult with the annotated
version, and removes the non-annotated version. All JSON parsing points
should now have annotated errors.
parent c8b662f1
......@@ -97,7 +97,7 @@ parseNode n a = do
parseData :: String -- ^ The JSON message as received from Ganeti
-> Result Request -- ^ A (possible valid) request
parseData body = do
decoded <- fromJResult $ decodeStrict body
decoded <- fromJResult "Parsing input IAllocator message" (decodeStrict body)
let obj = fromJSObject decoded
-- request parser
request <- liftM fromJSObject (fromObj "request" obj)
......
......@@ -130,5 +130,5 @@ loadData master = do -- IO monad
let (node_names, node_idx) = assignIndices node_data
inst_data <- inst_body >>= getInstances node_names
let (_, inst_idx) = assignIndices inst_data
tags_data <- tags_body >>= (fromJResult . decodeStrict)
tags_data <- tags_body >>= (fromJResult "Parsing tags data" . decodeStrict)
return (node_idx, inst_idx, tags_data)
......@@ -93,13 +93,9 @@ varianceCoeff lst =
-- * JSON-related functions
-- | Converts a JSON Result into a monadic value.
fromJResult :: Monad m => J.Result a -> m a
fromJResult (J.Error x) = fail x
fromJResult (J.Ok x) = return x
annotateJResult :: Monad m => String -> J.Result a -> m a
annotateJResult s (J.Error x) = fail (s ++ ": " ++ x)
annotateJResult _ (J.Ok x) = return x
fromJResult :: Monad m => String -> J.Result a -> m a
fromJResult s (J.Error x) = fail (s ++ ": " ++ x)
fromJResult _ (J.Ok x) = return x
-- | Tries to read a string from a JSON value.
--
......@@ -116,14 +112,14 @@ loadJSArray :: (Monad m)
=> String -- ^ Operation description (for error reporting)
-> String -- ^ Input message
-> m [J.JSObject J.JSValue]
loadJSArray s = annotateJResult s . J.decodeStrict
loadJSArray s = fromJResult 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
fromObj k o =
case lookup k o of
Nothing -> fail $ printf "key '%s' not found in %s" k (show o)
Just val -> annotateJResult (printf "key '%s', value '%s'" k (show val))
Just val -> fromJResult (printf "key '%s', value '%s'" k (show val))
(J.readJSON val)
-- | Annotate a Result with an ownership information
......
......@@ -150,7 +150,8 @@ buildCall msg args =
-- call was successful.
validateResult :: String -> Result JSValue
validateResult s = do
oarr <- fromJResult $ decodeStrict s::Result (JSObject JSValue)
oarr <- fromJResult "Parsing LUXI response"
(decodeStrict s)::Result (JSObject JSValue)
let arr = J.fromJSObject oarr
status <- fromObj (strOfKey Success) arr::Result Bool
let rkey = strOfKey Result
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment