Skip to content

error handling & testing #11

Answered by benbjohnson
dtirer asked this question in Q&A
Discussion options

You must be logged in to vote

Hi Daniel, great question. I used to use sentinel errors quite a bit but I've changed my approach to use code/message for a couple of reasons:

  1. It works well with error wrapping—e.g. fmt.Errorf("foo: %w, err). Ultimately I want to test what the end-user will see and not the wrapped error message (which I'll write to the log).

  2. It works well over a network boundary. If you use parse an incoming error message from another server using errors.New(s) then that error won't == the sentinel error even though they have the same text.

  3. Codes translate well to implementation-specific error codes (e.g. http or grpc).

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@dtirer
Comment options

Answer selected by benbjohnson
Comment options

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