forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
122 lines (101 loc) · 2.86 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
/* API errors are intended to be consumed in two ways. One, they can be dealt with as a single unit:
result, err := gateway.Create(transaction)
err.Error() => "A top level error message"
Second, you can drill down to see specific error messages on a field-by-field basis:
err.For("Transaction").On("Base")[0].Message => "A more specific error message"
*/
package braintree
import "strings"
type errorGroup interface {
For(string) errorGroup
On(string) []FieldError
}
type BraintreeError struct {
statusCode int
XMLName string `xml:"api-error-response"`
Errors responseErrors `xml:"errors"`
ErrorMessage string `xml:"message"`
MerchantAccount *MerchantAccount `xml:",omitempty"`
Transaction Transaction `xml:"transaction"`
}
func (e *BraintreeError) Error() string {
return e.ErrorMessage
}
func (e *BraintreeError) StatusCode() int {
return e.statusCode
}
func (e *BraintreeError) All() []FieldError {
baseErrors := e.Errors.TransactionErrors.ErrorList.Errors
creditCardErrors := e.Errors.TransactionErrors.CreditCardErrors.ErrorList.Errors
customerErrors := e.Errors.TransactionErrors.CustomerErrors.ErrorList.Errors
allErrors := append(baseErrors, creditCardErrors...)
allErrors = append(allErrors, customerErrors...)
return allErrors
}
func (e *BraintreeError) For(item string) errorGroup {
switch item {
default:
return nil
case "Transaction":
return e.Errors.TransactionErrors
}
}
func (e *BraintreeError) On(item string) []FieldError {
return []FieldError{}
}
type responseErrors struct {
TransactionErrors responseError `xml:"transaction"`
}
type responseError struct {
ErrorList errorList `xml:"errors"`
CreditCardErrors errorBlock `xml:"credit-card"`
CustomerErrors errorBlock `xml:"customer"`
}
func (r responseError) For(item string) errorGroup {
switch item {
default:
return nil
case "Base":
return r.ErrorList.Errors
case "Customer":
return r.CustomerErrors.ErrorList.Errors
case "CreditCard":
return r.CreditCardErrors.ErrorList.Errors
}
}
func (r responseError) On(item string) []FieldError {
switch item {
default:
return []FieldError{}
case "Base":
return r.ErrorList.Errors
case "Customer":
return r.CustomerErrors.ErrorList.Errors
case "CreditCard":
return r.CreditCardErrors.ErrorList.Errors
}
}
type errorBlock struct {
ErrorList errorList `xml:"errors"`
}
type errorList struct {
Errors FieldErrorList `xml:"error"`
}
type FieldErrorList []FieldError
func (f FieldErrorList) For(item string) errorGroup {
return nil
}
func (f FieldErrorList) On(item string) []FieldError {
errors := make([]FieldError, 0)
for _, e := range f {
if strings.ToLower(item) == e.Attribute {
errors = append(errors, e)
}
}
return errors
}
type FieldError struct {
Code string `xml:"code"`
Attribute string `xml:"attribute"`
Message string `xml:"message"`
}