Skip to content

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

@maralorn

Description

@maralorn

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions