glennsl/rescript-json-combinators

Expand decode error to include information about error

hellos3b opened this issue · 2 comments

Would be nice if the decode exception included information about the error instead of the error message, so the output could be formatted nicely to an end user

type path = array<string>
type expected = string
type actual = string

exception DecodeError(path, expected, actual)

The path property can point to where in an object or array that the error occured, so I can log a message like Expected "string" in Object.nested.field[0]

It's certainly possible to make the errors more strongly typed, but this scheme doesn't cover all the cases. How would you, for example, use this in oneOf.

It would also be a major breaking change. And I'm not sure how useful it really is, seeing as your use case of logging a string seems to be covered well by the current error representation.

But I'm open to suggestions and use cases that show its usefulness.

DZakh commented

In ReScript struct, it's represented as an InvalidUnion error code. Used the feature mostly for tooling, not in application code, e.g. to add custom error handling for specific parsing errors https://github.com/DZakh/rescript-stdlib-vendorer/blob/99fd057478c5d2596b6de48a0b26ad376808304b/src/interactors/RunCli.res#L48-L74.
Not sure whether it's a use case for rescript-json-combinators though.

image

image