-
Notifications
You must be signed in to change notification settings - Fork 332
Description
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.
aeson/src/Data/Aeson/Types/Internal.hs
Lines 566 to 567 in 45d31f1
| 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).
aeson/src/Data/Aeson/Types/Internal.hs
Lines 582 to 584 in 45d31f1
| 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
parseto also useformatError. (People can opt-in to the old behavior by usingiparseand dropping the path.) - Or to implement
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.