I was surprised to see that the quality of json parse errors in our program differed at different places. The discrepancy stemmed from using fromJSON and parseEither.
The reason is that parse ignores the json path when constructing the Result: (see const Error) here. The const drops info.
|
parse :: (a -> Parser b) -> a -> Result b |
|
parse m v = runParser (m v) [] (const Error) Success |
While parseEither adds the json path into the error message with onError path msg = Left (formatError path msg).
|
parseEither :: (a -> Parser b) -> a -> Either String b |
|
parseEither m v = runParser (m v) [] onError Right |
|
where onError path msg = Left (formatError path msg) |
This might very well be fully intentional, but again it took me by surprise. And my personal opinion is that the default should be the more verbose error.
I would thus suggest to either
- change the implementation of
parse to also use formatError. (People can opt-in to the old behavior by using iparse and dropping the path.)
- Or to implement
fromJSON in a way which uses formatError. E.g. by using iparse and applying formatError or on top of parseEither.
I see however that both suggestions might be contentious, so I am open to a discussion about this. If this idea is accept I would volunteer to implement the change.
I was surprised to see that the quality of json parse errors in our program differed at different places. The discrepancy stemmed from using
fromJSONandparseEither.The reason is that
parseignores the json path when constructing the Result: (seeconst Error) here. Theconstdrops info.aeson/src/Data/Aeson/Types/Internal.hs
Lines 566 to 567 in 45d31f1
While
parseEitheradds the json path into the error message withonError path msg = Left (formatError path msg).aeson/src/Data/Aeson/Types/Internal.hs
Lines 582 to 584 in 45d31f1
This might very well be fully intentional, but again it took me by surprise. And my personal opinion is that the default should be the more verbose error.
I would thus suggest to either
parseto also useformatError. (People can opt-in to the old behavior by usingiparseand dropping the path.)fromJSONin a way which usesformatError. E.g. by usingiparseand applyingformatErroror on top ofparseEither.I see however that both suggestions might be contentious, so I am open to a discussion about this. If this idea is accept I would volunteer to implement the change.