-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patherror.go
155 lines (136 loc) · 3.08 KB
/
error.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package yext
import (
"fmt"
"strconv"
"strings"
)
type ErrorType string
const (
ErrorTypeFatal = "FATAL_ERROR"
ErrorTypeNonFatal = "NON_FATAL_ERROR"
ErrorTypeWarning = "WARNING"
)
type Error struct {
Message string `json:"message"`
Code int `json:"code"`
Type string `json:"type"`
RequestUUID string `json:"request_uuid"`
}
func (e Error) Error() string {
return fmt.Sprintf("type: %s code: %d message: %s, request uuid: %s", e.Type, e.Code, e.Message, e.RequestUUID)
}
func (e Error) ErrorWithoutUUID() string {
return fmt.Sprintf("type: %s code: %d message: %s", e.Type, e.Code, e.Message)
}
func (e Error) IsError() bool {
return e.Type == ErrorTypeFatal || e.Type == ErrorTypeNonFatal
}
func (e Error) IsWarning() bool {
return e.Type == ErrorTypeWarning
}
type Errors []*Error
func (e Errors) Error() string {
var (
errs = make([]string, len(e))
uuid = ""
)
for i, err := range e {
errs[i] = err.ErrorWithoutUUID()
uuid = err.RequestUUID
}
return fmt.Sprintf("%s; request uuid: %s", strings.Join(errs, "; "), uuid)
}
func (e Errors) Errors() []*Error {
var errors []*Error
for _, err := range e {
if err.IsError() {
errors = append(errors, err)
}
}
return errors
}
func (e Errors) Warnings() []*Error {
var warnings []*Error
for _, err := range e {
if err.IsWarning() {
warnings = append(warnings, err)
}
}
return warnings
}
func IsNotFoundError(err error) bool {
if e, ok := err.(Errors); ok {
for _, innerError := range e {
if IsNotFoundError(innerError) {
return true
}
}
} else if e, ok := err.(*Error); ok {
if e.Code == 2000 || e.Code == 6004 {
return true
}
}
return false
}
func IsErrorCode(err error, code int) bool {
if e, ok := err.(Errors); ok {
for _, innerError := range e {
if IsErrorCode(innerError, code) {
return true
}
}
} else if e, ok := err.(*Error); ok {
if e.Code == code {
return true
}
}
return false
}
func splitStrAtWord(str string, word string) (string, string) {
var (
words = strings.Split(str, " ")
found = false
before = ""
after = ""
)
for _, w := range words {
if w == word {
found = true
} else if found {
if after != "" {
after += " "
}
after += w
} else {
if before != "" {
before += " "
}
before += w
}
}
return before, after
}
func errorFromString(str string) (*Error, error) {
strRemaining := strings.TrimLeft(str, "type: ")
typ, strRemaining := splitStrAtWord(strRemaining, "code:")
code, message := splitStrAtWord(strRemaining, "message:")
codeInt, errConv := strconv.Atoi(code)
if errConv != nil {
return nil, errConv
}
return &Error{Type: typ, Code: codeInt, Message: message}, nil
}
func ErrorsFromString(errorStr string) ([]*Error, error) {
errStrList := strings.Split(errorStr, "; ")
var errors []*Error
uuid := strings.TrimLeft(errStrList[len(errStrList)-1], "request uuid: ")
for i := 0; i < len(errStrList)-1; i++ {
errObj, err := errorFromString(errStrList[i])
if err != nil {
return nil, err
}
errObj.RequestUUID = uuid
errors = append(errors, errObj)
}
return errors, nil
}