Skip to content

Question: Handling errors with custom sub-types #1250

Answered by louthy
aloslider asked this question in Q&A
Discussion options

You must be logged in to vote

The reason why the codes are useful is exactly because of the matching. The codes will be compared when doing a match. This allows for localisation of text and parameterised errors (where the content of the message changes) , or sharing of codes, etc. It's also very fast.

You can use 0 as the code if you like, then the Message is compared - so if you have unique error-messages it will work fine with subtypes. But I'd encourage you to leverage the codes.

Also, you only need sub-typed errors if you're going to be carrying extra data, otherwise I'd encourage you to use the Error.New(...) variants to create errors (again leveraging the code as a discriminator rather than the type).

What I ten…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by aloslider
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants