rescript-json-combinators icon indicating copy to clipboard operation
rescript-json-combinators copied to clipboard

Expand decode error to include information about error

Open hellos3b opened this issue 2 years ago • 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]

hellos3b avatar Jan 03 '23 18:01 hellos3b

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.

glennsl avatar Jan 08 '23 13:01 glennsl

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

DZakh avatar Jan 16 '23 10:01 DZakh