@@ -38,12 +38,29 @@ func newResponseError(resp *http.Response, target int, targets ...int) error {
38
38
}
39
39
}
40
40
41
- // IsResponseError is convenience function to see
42
- // if it can convert into RequestError.
43
- func IsResponseError (err error ) (* ResponseError , bool ) {
44
- var re * ResponseError
45
- if errors .As (err , & re ) {
46
- return err .(* ResponseError ), true
41
+ // AsResponseError is a convenience function to check the error
42
+ // to see if it contains an `ResponseError` and returns the value with true.
43
+ // If the error was initially joined using [errors.Join], it will check each error
44
+ // within the list and return the first matching error.
45
+ func AsResponseError (err error ) (* ResponseError , bool ) {
46
+ // When `errors.Join` is called, it returns an error that
47
+ // matches the provided interface.
48
+ if joined , ok := err .(interface { Unwrap () []error }); ok {
49
+ for _ , err := range joined .Unwrap () {
50
+ if re , ok := AsResponseError (err ); ok {
51
+ return re , ok
52
+ }
53
+ }
54
+ return nil , false
55
+ }
56
+
57
+ for err != nil {
58
+ if re , ok := err .(* ResponseError ); ok {
59
+ return re , true
60
+ }
61
+ // In case the error is wrapped using `fmt.Errorf`
62
+ // this will also account for that.
63
+ err = errors .Unwrap (err )
47
64
}
48
65
return nil , false
49
66
}
0 commit comments