-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
102 lines (90 loc) · 2.6 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
package awql
import (
"encoding/xml"
"strings"
)
// Error messages.
var (
ErrQuery = NewQueryError("missing")
ErrQueryBinding = NewQueryError("binding not match")
ErrNoDsn = NewConnectionError("missing data source")
ErrNoNetwork = NewConnectionError("not found")
ErrBadNetwork = NewConnectionError("service unavailable")
ErrBadToken = NewConnectionError("invalid access token")
ErrAdwordsID = NewConnectionError("adwords id")
ErrDevToken = NewConnectionError("developer token")
)
// APIError represents a Google Report Download Error.
// It voluntary ignores trigger field.
//
// In case of error, Google Adwords API provides more information in a XML response:
//
// <reportDownloadError>
// <ApiError>
// <type>ReportDefinitionError.CUSTOMER_SERVING_TYPE_REPORT_MISMATCH</type>
// <trigger></trigger>
// <fieldPath>selector</fieldPath>
// </ApiError>
// </reportDownloadError>
//
type APIError struct {
Type string `xml:"ApiError>type"`
Trigger string `xml:"ApiError>trigger"`
Field string `xml:"ApiError>fieldPath"`
}
// NewAPIError parses a XML document that represents a download report error.
// It returns the given message as error.
func NewAPIError(d []byte) error {
if len(d) == 0 {
return ErrNoDsn
}
e := &APIError{}
err := xml.Unmarshal(d, e)
if err != nil {
e.Type = err.Error()
}
return e
}
// String returns a representation of the api error.
func (e *APIError) Error() string {
switch e.Field {
case "":
if e.Trigger == "" || e.Trigger == "<null>" {
return e.Type
}
return e.Type + " (" + e.Trigger + ")"
case "selector":
return e.Type
default:
return e.Type + " on " + e.Field
}
}
// ConnectionError represents an connection error.
type ConnectionError struct {
s string
}
// NewConnectionError returns an error of type Connection with the given text.
func NewConnectionError(text string) error {
return &ConnectionError{formatError(text)}
}
// Error outputs a connection error message.
func (e *ConnectionError) Error() string {
return "ConnectionError." + e.s
}
// QueryError represents a query error.
type QueryError struct {
s string
}
// NewQueryError returns an error of type Internal with the given text.
func NewQueryError(text string) error {
return &QueryError{formatError(text)}
}
// Error outputs a query error message.
func (e *QueryError) Error() string {
return "QueryError." + e.s
}
// formatError returns a string in upper case with underscore instead of space.
// As the Adwords API outputs its errors.
func formatError(s string) string {
return strings.Replace(strings.ToUpper(strings.TrimSpace(s)), " ", "_", -1)
}