forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
206 lines (185 loc) · 4.31 KB
/
errors.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package braintree
import (
"bytes"
"encoding/xml"
"io"
"strconv"
"unicode"
)
type BraintreeError struct {
statusCode int
errors ValidationErrors
ErrorMessage string
MerchantAccount *MerchantAccount
Transaction *Transaction
}
func (e *BraintreeError) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var x struct {
Errors ValidationErrors `xml:"errors"`
ErrorMessage string `xml:"message"`
MerchantAccount *MerchantAccount `xml:"merchant-account"`
Transaction *Transaction `xml:"transaction"`
}
err := d.DecodeElement(&x, &start)
if err != nil {
return err
}
e.errors = x.Errors
e.ErrorMessage = x.ErrorMessage
e.MerchantAccount = x.MerchantAccount
e.Transaction = x.Transaction
return nil
}
func (e *BraintreeError) Error() string {
return e.ErrorMessage
}
func (e *BraintreeError) StatusCode() int {
return e.statusCode
}
func (e *BraintreeError) All() []ValidationError {
return e.errors.AllDeep()
}
func (e *BraintreeError) For(name string) *ValidationErrors {
return e.errors.For(name)
}
type ValidationErrors struct {
Object string
ValidationErrors []ValidationError
Children map[string]*ValidationErrors
}
func (r *ValidationErrors) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
} else if err != nil {
return err
}
r.Object = errorNameKebabToCamel(start.Name.Local)
if subStart, ok := t.(xml.StartElement); ok {
subName := subStart.Name.Local
if subName == "errors" {
errorList := struct {
ErrorList []ValidationError `xml:"error"`
}{}
err := d.DecodeElement(&errorList, &subStart)
if err != nil {
return err
}
r.ValidationErrors = errorList.ErrorList
} else {
subSectionName := errorNameKebabToCamel(subName)
subSection := &ValidationErrors{}
err := d.DecodeElement(subSection, &subStart)
if err != nil {
return err
}
if r.Children == nil {
r.Children = map[string]*ValidationErrors{}
}
r.Children[subSectionName] = subSection
}
}
}
return nil
}
func (r *ValidationErrors) All() []ValidationError {
if r == nil {
return nil
}
return r.ValidationErrors
}
func (r *ValidationErrors) AllDeep() []ValidationError {
if r == nil {
return nil
}
errorList := append([]ValidationError{}, r.All()...)
for _, sub := range r.Children {
errorList = append(errorList, sub.AllDeep()...)
}
return errorList
}
func (r *ValidationErrors) For(name string) *ValidationErrors {
if r == nil || r.Children == nil {
return (*ValidationErrors)(nil)
}
return r.Children[name]
}
func (r *ValidationErrors) ForIndex(i int) *ValidationErrors {
if r == nil || r.Children == nil {
return (*ValidationErrors)(nil)
}
return r.Children["Index"+strconv.Itoa(i)]
}
func (r *ValidationErrors) On(name string) []ValidationError {
if r == nil {
return nil
}
errors := make([]ValidationError, 0)
for _, e := range r.ValidationErrors {
if name == e.Attribute {
errors = append(errors, e)
}
}
return errors
}
type ValidationError struct {
Code string
Attribute string
Message string
}
func (e *ValidationError) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var x struct {
Code string `xml:"code"`
Attribute string `xml:"attribute"`
Message string `xml:"message"`
}
err := d.DecodeElement(&x, &start)
if err != nil {
return err
}
e.Code = x.Code
e.Attribute = errorNameSnakeToCamel(x.Attribute)
e.Message = x.Message
return nil
}
func errorNameSnakeToCamel(snake string) string {
if len(snake) == 0 {
return ""
}
camel := bytes.Buffer{}
capitalizeNext := true
for i := 0; i < len(snake); i++ {
s := snake[i]
if s == '_' {
capitalizeNext = true
continue
} else if capitalizeNext {
camel.WriteByte(byte(unicode.ToUpper(rune(s))))
} else {
camel.WriteByte(s)
}
capitalizeNext = false
}
return camel.String()
}
func errorNameKebabToCamel(kebab string) string {
if len(kebab) == 0 {
return ""
}
camel := bytes.Buffer{}
capitalizeNext := true
for i := 0; i < len(kebab); i++ {
k := kebab[i]
if k == '-' {
capitalizeNext = true
continue
} else if capitalizeNext {
camel.WriteByte(byte(unicode.ToUpper(rune(k))))
} else {
camel.WriteByte(k)
}
capitalizeNext = false
}
return camel.String()
}