Skip to content

Commit

Permalink
Merge pull request #695 from FintanH/fintan/better-errors
Browse files Browse the repository at this point in the history
Add labels to json object and list parsing
  • Loading branch information
bergmark authored Apr 25, 2019
2 parents d95d985 + d1364e4 commit 3f8a420
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
17 changes: 11 additions & 6 deletions Data/Aeson/Parser/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ objectValues str val = do
-- Why use acc pattern here, you may ask? because 'H.fromList' use 'unsafeInsert'
-- and it's much faster because it's doing in place update to the 'HashMap'!
loop acc = do
k <- str <* skipSpace <* char ':'
v <- val <* skipSpace
ch <- A.satisfy $ \w -> w == COMMA || w == CLOSE_CURLY
k <- (str A.<?> "object key") <* skipSpace <* (char ':' A.<?> "':'")
v <- (val A.<?> "object value") <* skipSpace
ch <- A.satisfy (\w -> w == COMMA || w == CLOSE_CURLY) A.<?> "',' or '}'"
let acc' = (k, v) : acc
if ch == COMMA
then skipSpace >> loop acc'
Expand All @@ -149,8 +149,8 @@ arrayValues val = do
else loop [] 1
where
loop acc !len = do
v <- val <* skipSpace
ch <- A.satisfy $ \w -> w == COMMA || w == CLOSE_SQUARE
v <- (val A.<?> "json list value") <* skipSpace
ch <- A.satisfy (\w -> w == COMMA || w == CLOSE_SQUARE) A.<?> "',' or ']'"
if ch == COMMA
then skipSpace >> loop (v:acc) (len+1)
else return (Vector.reverse (Vector.fromListN len (v:acc)))
Expand Down Expand Up @@ -275,7 +275,12 @@ eitherDecodeWith p to s =
L.Done _ v -> case to v of
ISuccess a -> Right a
IError path msg -> Left (path, msg)
L.Fail _ _ msg -> Left ([], msg)
L.Fail _ ctx msg -> Left ([], buildMsg ctx msg)
where
buildMsg :: [String] -> String -> String
buildMsg [] msg = msg
buildMsg (expectation:_) msg =
msg ++ ". Expecting " ++ expectation
{-# INLINE eitherDecodeWith #-}

eitherDecodeStrictWith :: Parser Value -> (Value -> IResult a) -> B.ByteString
Expand Down
5 changes: 5 additions & 0 deletions tests/ErrorMessages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ outputGeneric choice = concat
, "{\"record\": {}, \"W\":{}}"
, "{}"
, "[]"
, "{\"unary\""
, "{\"unary\":"
, "{\"unary\":1"
]

, testWithSomeType "SomeType (two-element array)"
Expand All @@ -129,6 +132,8 @@ outputGeneric choice = concat
, "[null, 0]"
, "[]"
, "{}"
, "[1"
, "[1,"
]

, testWith "EitherTextInt"
Expand Down
5 changes: 5 additions & 0 deletions tests/golden/generic.expected
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ Error in $: parsing Types.SomeType failed, expected an Object with a single pair
Error in $: parsing Types.SomeType failed, expected an Object with a single pair, but found 2 pairs
Error in $: parsing Types.SomeType failed, expected an Object with a single pair, but found 0 pairs
Error in $: parsing Types.SomeType failed, expected Object, but encountered Array
Error in $: not enough input. Expecting ':'
Error in $: not enough input. Expecting object value
Error in $: not enough input. Expecting ',' or '}'
SomeType (two-element array)
Error in $[1]: parsing Int failed, expected Number, but encountered Boolean
Error in $[1]: parsing Types.SomeType(Record) failed, expected Object, but encountered Null
Error in $[0]: parsing Types.SomeType failed, expected tag of the 2-element Array to be one of ["nullary","unary","product","record","list"], but found tag "X"
Error in $[0]: parsing Types.SomeType failed, tag element is not a String
Error in $: parsing Types.SomeType failed, expected a 2-element Array, but encountered an Array of length 0
Error in $: parsing Types.SomeType failed, expected Array, but encountered Object
Error in $: not enough input. Expecting ',' or ']'
Error in $: not enough input. Expecting json list value
EitherTextInt
Error in $: parsing Types.EitherTextInt(NoneNullary) failed, expected tag "nonenullary", but found tag "X"
Error in $: parsing Types.EitherTextInt(NoneNullary) failed, expected String, but encountered Array
Expand Down

0 comments on commit 3f8a420

Please sign in to comment.