-
Notifications
You must be signed in to change notification settings - Fork 54
/
errors.go
151 lines (136 loc) · 4.1 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
package gads
import (
"encoding/xml"
"fmt"
"strings"
)
type OperationError struct {
Code int64 `xml:"OperationError>Code"`
Details string `xml:"OperationError>Details"`
ErrorCode string `xml:"OperationError>ErrorCode"`
Message string `xml:"OperationError>Message"`
}
type BudgetError struct {
Path string `xml:"fieldPath"`
String string `xml:"errorString"`
Trigger string `xml:"trigger"`
Reason string `xml:"reason"`
}
type CriterionError struct {
FieldPath string `xml:"fieldPath"`
Trigger string `xml:"trigger"`
ErrorString string `xml:"errorString"`
Reason string `xml:"reason"`
}
type TargetError struct {
FieldPath string `xml:"fieldPath"`
Trigger string `xml:"trigger"`
ErrorString string `xml:"errorString"`
Reason string `xml:"reason"`
}
type AdGroupServiceError struct {
FieldPath string `xml:"fieldPath"`
Trigger string `xml:"trigger"`
ErrorString string `xml:"errorString"`
Reason string `xml:"reason"`
}
type NotEmptyError struct {
FieldPath string `xml:"fieldPath"`
Trigger string `xml:"trigger"`
ErrorString string `xml:"errorString"`
Reason string `xml:"reason"`
}
type AdError struct {
FieldPath string `xml:"fieldPath"`
Trigger string `xml:"trigger"`
ErrorString string `xml:"errorString"`
Reason string `xml:"reason"`
}
// if you exceed the quota given by google
type RateExceededError struct {
RateName string `xml:"rateName"` // For example OperationsByMinute
RateScope string `xml:"rateScope"` // ACCOUNT or DEVELOPER
ErrorString string `xml:"errorString"`
Reason string `xml:"reason"`
RetryAfterSeconds uint `xml:"retryAfterSeconds"` // Try again in...
}
type ApiExceptionFault struct {
Message string `xml:"message"`
Type string `xml:"ApplicationException.Type"`
Errors []interface{} `xml:"errors"`
}
func (aes *ApiExceptionFault) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) (err error) {
for token, err := dec.Token(); err == nil; token, err = dec.Token() {
switch start := token.(type) {
case xml.StartElement:
switch start.Name.Local {
case "message":
if err := dec.DecodeElement(&aes.Message, &start); err != nil {
return err
}
case "ApplicationException.Type":
if err := dec.DecodeElement(&aes.Type, &start); err != nil {
return err
}
case "errors":
errorType, _ := findAttr(start.Attr, xml.Name{Space: "http://www.w3.org/2001/XMLSchema-instance", Local: "type"})
switch errorType {
case "CriterionError":
e := CriterionError{}
dec.DecodeElement(&e, &start)
aes.Errors = append(aes.Errors, e)
case "TargetError":
e := TargetError{}
dec.DecodeElement(&e, &start)
aes.Errors = append(aes.Errors, e)
case "BudgetError":
e := BudgetError{}
dec.DecodeElement(&e, &start)
aes.Errors = append(aes.Errors, e)
case "AdGroupServiceError":
e := AdGroupServiceError{}
dec.DecodeElement(&e, &start)
aes.Errors = append(aes.Errors, e)
case "NotEmptyError":
e := NotEmptyError{}
dec.DecodeElement(&e, &start)
aes.Errors = append(aes.Errors, e)
case "AdError":
e := AdError{}
dec.DecodeElement(&e, &start)
aes.Errors = append(aes.Errors, e)
case "RateExceededError":
e := RateExceededError{}
dec.DecodeElement(&e, &start)
aes.Errors = append(aes.Errors, e)
default:
return fmt.Errorf("Unknown error type -> %s", start)
}
case "reason":
break
default:
return fmt.Errorf("Unknown error field -> %s", start)
}
}
}
return err
}
type ErrorsType struct {
ApiExceptionFaults []ApiExceptionFault `xml:"ApiExceptionFault"`
}
func (f ErrorsType) Error() string {
errors := []string{}
for _, e := range f.ApiExceptionFaults {
errors = append(errors, fmt.Sprintf("%s", e.Message))
}
return strings.Join(errors, "\n")
}
type Fault struct {
XMLName xml.Name `xml:"Fault"`
FaultCode string `xml:"faultcode"`
FaultString string `xml:"faultstring"`
Errors ErrorsType `xml:"detail"`
}
func (f Fault) Error() string {
return f.FaultString + " - " + f.Errors.Error()
}