Suggestion: Let error String of fromJSON contain json path info.
- Dominant language
- Haskell
- Stars
- 1.3k
- Forks
- 336
- Avg merge
- 3d 7h
- Merged PRs (30d)
- 4
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.
https://github.com/haskell/aeson/blob/45d31f1bd9a0edbd6ba55fbcdd5082b159d33106/src/Data/Aeson/Types/Internal.hs#L566-L567
While `parseEither` adds the json path into the error message with `onError path msg = Left (formatError path msg)`.
https://github.com/haskell/aeson/blob/45d31f1bd9a0edbd6ba55fbcdd5082b159d33106/src/Data/Aeson/Types/Internal.hs#L582-L584
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.
Contributor guide
Assessment
This issue has not been assessed yet.