If I’m creating an error likefmt.Errorf("%s must be foo, got %s", bar, baz)
, is there a way to test equality without comparing the string value?
StatusTeapot
and then we use its URL as the argument to DumbGetter
so we can see it handles non 200
responses correctly.How would I want to use this code?
DumbGetter
is provide a way for users to use the type system to understand what kind of error has happened.DumbGetter
could return us something likeBadStatusError
implement the error interface.BadStatusError
. This reflects our desire for the kind of error clearer. Assuming the assertion passes we can then check the properties of the error are correct.DumbGetter
by updating our error handling code to use our typeDumbGetter
function has become simpler, it's no longer concerned with the intricacies of an error string, it just creates a BadStatusError
.error
, so if they choose to they can pass it up the call stack or log it like any other error
.errors.As
to try and extract our error into our custom type. It returns a bool
to denote success and extracts it into got
for us.