MCPcopy
hub / github.com/stretchr/testify / ErrorAs

Function ErrorAs

assert/assertions.go:2221–2241  ·  view source on GitHub ↗

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{})

Source from the content-addressed store, hash-verified

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.
2221func 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.

Callers 4

ErrorAsFunction · 0.92
ErrorAsfFunction · 0.70
TestErrorAsFunction · 0.70
ErrorAsMethod · 0.70

Calls 4

buildErrorChainStringFunction · 0.85
FailFunction · 0.70
HelperMethod · 0.65
StringMethod · 0.45

Tested by 1

TestErrorAsFunction · 0.56