I am making a small function to check an error against some types and decide (depending on the error type) whether some retries are worth it.
I have created my own temporary interface to type assert all net/http errors that implement this interface (which however is unexported and thus I am declaring it my code as well)
type temporary interface {
Temporary() bool
}
So, I want to retry when there is a net/http error implementing the temporary interface, as well in case the error is one of the following types: io.EOF, io.ErrUnexpectedEOF of ErrTimeoutExceeded
where
var ErrTimeoutExceeded = errors.New("timeout exceeded")
however the following switch statement
func isWorthRetrying(err error) bool {
switch e := err.(type) {
case temporary:
return true
case io.EOF:
return true
case io.ErrUnexpectedEOF:
return true
case ErrTimeoutExceeded:
return true
default:
return false
}
}
errors out in all statements (except the one type asserting against temporary)
with the error e.g.
io.EOF (type error) is not a type
What does this mean?
io.EOFis a value, not a type.switch e := err.(type)is a type switch for asserting the types in the cases. You are mixing up type switch and normal (expression) switch. You need two separate switches, one for types one for the error values.