How to assert on error types in Go tests?

Anirudh Jayakumar :

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?

Burak Serdar :

Use type assertion:

err := someFunc()
if retryable, ok := err.(RetryableError); ok {
   // use retryable
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related