Skip to content

Use error structs instead of duplicated strings #102

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

Merged
merged 2 commits into from
Jun 16, 2025

Conversation

m1k1o
Copy link

@m1k1o m1k1o commented Jun 14, 2025

Currently, all errors are just strings. They have all one thing in common: They identify a field that caused the error. Therefore I created DecodeError struct that hold this information.

The next batch of errors is about parsing a certain type. They all have the information about why the parsing failed and additionally what was the expected and actual value. Therefore I created ParseError.

Last similarity in all errors that I found was when handling unconvertible types. In contrast to parsing errors, they do not have any parsing error as the appropriate parser could not be even found. Therefore I created UnconvertibleTypeError for them.

This allows us to get detailed errors about what went wrong in the decoding process, it helps with the code clarity - lot of duplicate messages were joined to a single struct. And more importantly, what was my main motivation, it allows me to join the errors together when I use custom nested decoder.

@sagikazarmark
Copy link
Member

Hey @m1k1o

Thanks a lot for this PR.

You just implemented error handling exactly how I wanted to. 😄

@sagikazarmark sagikazarmark merged commit f12f6c7 into go-viper:main Jun 16, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants