This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
errors.go
106 lines (84 loc) · 2.29 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
package contentful
import (
"bytes"
"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 {
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 := bytes.Buffer{}
for _, err := range e.APIError.err.Details.Errors {
if err.Name == "uniqueFieldIds" || err.Name == "uniqueFieldApiNames" {
return msg.String()
}
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.APIError.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{}