Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand decode error to include information about error #2

Closed
hellos3b opened this issue Jan 3, 2023 · 2 comments
Closed

Expand decode error to include information about error #2

hellos3b opened this issue Jan 3, 2023 · 2 comments

Comments

@hellos3b
Copy link

hellos3b commented Jan 3, 2023

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]

@glennsl
Copy link
Owner

glennsl commented Jan 8, 2023

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
Copy link

DZakh commented Jan 16, 2023

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

@hellos3b hellos3b closed this as completed Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants