ErrorAs asserts that at least one of the errors in err's chain matches target, and if so, sets target to that error value. This is a wrapper for errors.As.
(t TestingT, err error, target interface{}, msgAndArgs ...interface{})
| 2219 | // ErrorAs asserts that at least one of the errors in err's chain matches target, and if so, sets target to that error value. |
| 2220 | // This is a wrapper for errors.As. |
| 2221 | func ErrorAs(t TestingT, err error, target interface{}, msgAndArgs ...interface{}) bool { |
| 2222 | if h, ok := t.(tHelper); ok { |
| 2223 | h.Helper() |
| 2224 | } |
| 2225 | if errors.As(err, target) { |
| 2226 | return true |
| 2227 | } |
| 2228 | |
| 2229 | expectedType := reflect.TypeOf(target).Elem().String() |
| 2230 | if err == nil { |
| 2231 | return Fail(t, fmt.Sprintf("An error is expected but got nil.\n"+ |
| 2232 | "expected: %s", expectedType), msgAndArgs...) |
| 2233 | } |
| 2234 | |
| 2235 | chain := buildErrorChainString(err, true) |
| 2236 | |
| 2237 | return Fail(t, fmt.Sprintf("Should be in error chain:\n"+ |
| 2238 | "expected: %s\n"+ |
| 2239 | "in chain: %s", expectedType, chain, |
| 2240 | ), msgAndArgs...) |
| 2241 | } |
| 2242 | |
| 2243 | // NotErrorAs asserts that none of the errors in err's chain matches target, |
| 2244 | // but if so, sets target to that error value. |