-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy patherrs.go
48 lines (40 loc) · 1.16 KB
/
errs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package errs
import (
"errors"
"strings"
"github.com/aws/aws-sdk-go/aws/awserr"
)
// Messager is a simple interface for types with ErrorMessage().
type Messager interface {
ErrorMessage() string
}
func AsContains(err error, target any, message string) bool {
if errors.As(err, target) {
if v, ok := target.(Messager); ok && strings.Contains(v.ErrorMessage(), message) {
return true
}
}
return false
}
// Contains returns true if the error matches all these conditions:
// - err as string contains needle
func Contains(err error, needle string) bool {
if err != nil && strings.Contains(err.Error(), needle) {
return true
}
return false
}
// MessageContains unwraps the error and returns true if the error matches
// all these conditions:
// - err is of type awserr.Error, Error.Code() equals code, and Error.Message() contains message
// - OR err if not of type awserr.Error as string contains both code and message
func MessageContains(err error, code string, message string) bool {
var awsErr awserr.Error
if AsContains(err, &awsErr, message) {
return true
}
if Contains(err, code) && Contains(err, message) {
return true
}
return false
}