From 7ff4c91a2f16ba7ca6a76698dfa44a0030baaf45 Mon Sep 17 00:00:00 2001 From: Harry Garrood Date: Wed, 13 Jan 2016 17:04:49 +0000 Subject: [PATCH] Improve generic decoding errors * Include the type constructor name for all errors when decoding a SigProd * Distinguish between the 'tag' property missing, and it being the wrong type --- src/Data/Argonaut/Decode.purs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Data/Argonaut/Decode.purs b/src/Data/Argonaut/Decode.purs index 180d908..db22df2 100644 --- a/src/Data/Argonaut/Decode.purs +++ b/src/Data/Argonaut/Decode.purs @@ -51,13 +51,14 @@ gDecodeJson' signature json = case signature of sp <- gDecodeJson' (val unit) pf pure { recLabel: lbl, recValue: const sp } SigProd typeConstr alts -> do - let decodingErr msg = "When decoding " ++ typeConstr ++ " " ++ msg + let decodingErr msg = "When decoding a " ++ typeConstr ++ ": " ++ msg jObj <- mFail (decodingErr "expected an object") (toObject json) - tag <- mFail (decodingErr "'tag' string property is missing") (toString =<< M.lookup "tag" jObj) + tagJson <- mFail (decodingErr "'tag' property is missing") (M.lookup "tag" jObj) + tag <- mFail (decodingErr "'tag' property is not a string") (toString tagJson) case find ((tag ==) <<< _.sigConstructor) alts of - Nothing -> Left ("'" <> tag <> "' isn't a valid constructor") + Nothing -> Left (decodingErr ("'" <> tag <> "' isn't a valid constructor")) Just { sigValues: sigValues } -> do - vals <- mFail "'values' array is missing" (toArray =<< M.lookup "values" jObj) + vals <- mFail (decodingErr "'values' array is missing") (toArray =<< M.lookup "values" jObj) sps <- zipWithA (\k -> gDecodeJson' (k unit)) sigValues vals pure (SProd tag (const <$> sps)) where