haskell / haskell/aeson

Suggestion: Let error String of fromJSON contain json path info.

Open
#1,154 7 comments 1 reaction 0 assignees View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.