I have a error type like below defined
type RetryableError struct {
msg string
}
func (a *RetryableError) Error() string {
return a.msg
}
In a unit test, what is the Go way of asserting if the error returned is of RetryableError
type?
Use type assertion:
err := someFunc()
if retryable, ok := err.(*RetryableError); ok {
// use retryable
}
Your RetryableError
is not an error, but *RetryableError
is. To correct:
func (a RetryableError) Error() string {
return a.msg
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With