-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
129 lines (108 loc) · 2.76 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
package gontentful
import (
"net/http"
"encoding/json"
)
// ErrorResponse model
type ErrorResponse struct {
Sys *Sys `json:"sys"`
Message string `json:"message,omitempty"`
RequestID string `json:"requestId,omitempty"`
Details *ErrorDetails `json:"details,omitempty"`
}
func (e ErrorResponse) Error() string {
return e.Message
}
// ErrorDetails model
type ErrorDetails struct {
Errors []*ErrorDetail `json:"errors,omitempty"`
}
// ErrorDetail model
type ErrorDetail struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Path interface{} `json:"path,omitempty"`
Details string `json:"details,omitempty"`
Value interface{} `json:"value,omitempty"`
}
// APIError model
type APIError struct {
req *http.Request
res *http.Response
err *ErrorResponse
}
// AccessTokenInvalidError for 401 errors
type AccessTokenInvalidError struct {
APIError
}
func (e AccessTokenInvalidError) Error() string {
return e.APIError.err.Message
}
// VersionMismatchError for 409 errors
type VersionMismatchError struct {
APIError
}
func (e VersionMismatchError) Error() string {
return "Version " + e.APIError.req.Header.Get("X-Contentful-Version") + " is mismatched"
}
// ValidationFailedError model
type ValidationFailedError struct {
APIError
}
func (e ValidationFailedError) Error() string {
msg := ""
for _, err := range e.APIError.err.Details.Errors {
if err.Name == "uniqueFieldIds" || err.Name == "uniqueFieldApiNames" {
return msg
// msg += err.Value["id"].(string) + " should be unique for " + err.Value["name"].(string) + "\n"
}
}
return msg
}
// NotFoundError for 404 errors
type NotFoundError struct {
APIError
}
func (e NotFoundError) Error() string {
return "the requested resource can not be found"
}
// RateLimitExceededError for rate limit errors
type RateLimitExceededError struct {
APIError
}
func (e RateLimitExceededError) Error() string {
return e.APIError.err.Message
}
type BadRequestError struct{}
type InvalidQueryError struct{}
type AccessDeniedError struct{}
type ServerError struct{}
func parseError(req *http.Request, res *http.Response) error {
var e ErrorResponse
defer res.Body.Close()
err := json.NewDecoder(res.Body).Decode(&e)
if err != nil {
return err
}
apiError := APIError{
req: req,
res: res,
err: &e,
}
switch errType := e.Sys.ID; errType {
case "NotFound":
return NotFoundError{apiError}
case "RateLimitExceeded":
return RateLimitExceededError{apiError}
case "AccessTokenInvalid":
return AccessTokenInvalidError{apiError}
case "ValidationFailed":
return ValidationFailedError{apiError}
case "VersionMismatch":
return VersionMismatchError{apiError}
case "Conflict":
return VersionMismatchError{apiError}
default:
return e
}
}