Hacker News new | ask | show | jobs
by cube2222 2869 days ago
There's another one I often use:

Create a custom error type, for example DB Error:

  type DBError struct {
     Temporary bool
     NetworkBased bool
     Cause error
  }

Now you can provide functions like IsTemporary(err).

Otherwise, you can use 2# with a twist, instead of matching on a type, you can do:

  switch {
     case isErrFetchForbidden(err):
     case isErrFetchNotFound(err):
  }
or even:

  IsBadRequest(err)
  IsInternal(err)
  IsTimeout(err)
1 comments

So you then define your function to return the type DBError instead of a generic err type. That makes sense to me but for some reason some of the stuff I've suggests that just returning err is more go-like.
No, you don't, DBError should implement the error interface.

IsTemporary also takes an error and does something like:

  if err, ok := errors.Cause(err).(*DBError) {
    return err.temporary
  } else {
    return false
  }
oooh!