forked from ohookins/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherrors.go
More file actions
136 lines (113 loc) · 3.12 KB
/
errors.go
File metadata and controls
136 lines (113 loc) · 3.12 KB
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
package contentful
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
// 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 {
byt, err := json.Marshal(e)
if err != nil {
return fmt.Sprintf("error marshaling ErrorResponse: %v - original error message: %v", err, e.Message)
}
return string(byt)
}
// Error defines an internal contentful data model error when e.g. an assets can
// be resolved. The structure gets returned while this error is set too. The
// programmer can decide how to handle the response by checking the
// Collection.Errors field.
type Error struct {
Sys *Sys
Details map[string]string
}
// 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.err.Message
}
// VersionMismatchError for 409 errors
type VersionMismatchError struct {
APIError
}
func (e VersionMismatchError) Error() string {
return "Version " + e.req.Header.Get("X-Contentful-Version") + " is mismatched"
}
// ValidationFailedError model
type ValidationFailedError struct {
APIError
}
func (e ValidationFailedError) Error() string {
msg := bytes.Buffer{}
for _, err := range e.err.Details.Errors {
switch err.Name {
case "uniqueFieldIds", "uniqueFieldApiNames":
return msg.String()
case "notResolvable":
if err.Path != nil {
switch path := err.Path.(type) {
case []interface{}:
pathString := ""
for _, segment := range path {
switch segment.(type) {
case string:
pathString += fmt.Sprintf("/%s", segment)
}
}
_, _ = msg.WriteString(fmt.Sprintf("errorName: %s, path: %s", err.Name, pathString))
}
}
default:
_, _ = msg.WriteString(fmt.Sprintf("%s\n", err.Details))
}
}
return msg.String()
}
// 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.err.Message
}
// BadRequestError error model for bad request responses
type BadRequestError struct{}
// InvalidQueryError error model for invalid query responses
type InvalidQueryError struct{}
// AccessDeniedError error model for access denied responses
type AccessDeniedError struct{}
// ServerError error model for server error responses
type ServerError struct{}