4 ms·
There's another one I often use: Create a custom error type, for example DB Error: type DBError struct { Temporary bool NetworkBased bool Cau
by cube2222 8y 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)
- macrael 8y agoSo 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.