Skip to content

Improve generic decoding errors #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Data/Argonaut/Decode.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down