-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
157 lines (133 loc) · 4.06 KB
/
Copy patherrors.go
File metadata and controls
157 lines (133 loc) · 4.06 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package gitcode
import (
"fmt"
"net/http"
)
// APIError represents a structured GitCode API error.
type APIError struct {
StatusCode int `json:"status_code"`
Message string `json:"message"`
URL string `json:"url,omitempty"`
Method string `json:"method,omitempty"`
Body string `json:"body,omitempty"`
}
func (e *APIError) Error() string {
return fmt.Sprintf("GitCode API %s %s returned %d: %s", e.Method, e.URL, e.StatusCode, e.Message)
}
// NotFoundError represents a 404 Not Found error.
type NotFoundError struct {
*APIError
Resource string `json:"resource,omitempty"` // e.g. "repository", "issue", "user"
}
func (e *NotFoundError) Error() string {
if e.Resource != "" {
return fmt.Sprintf("GitCode API: %s not found (%s %s)", e.Resource, e.Method, e.URL)
}
return e.APIError.Error()
}
// UnauthorizedError represents a 401 Unauthorized error.
type UnauthorizedError struct {
*APIError
}
func (e *UnauthorizedError) Error() string {
return fmt.Sprintf("GitCode API: unauthorized (%s %s) - check your token", e.Method, e.URL)
}
// ForbiddenError represents a 403 Forbidden error.
type ForbiddenError struct {
*APIError
}
func (e *ForbiddenError) Error() string {
return fmt.Sprintf("GitCode API: forbidden (%s %s) - insufficient permissions", e.Method, e.URL)
}
// RateLimitError represents a 429 Too Many Requests error.
type RateLimitError struct {
*APIError
RetryAfter int `json:"retry_after,omitempty"` // seconds to wait
}
func (e *RateLimitError) Error() string {
if e.RetryAfter > 0 {
return fmt.Sprintf("GitCode API: rate limited (%s %s) - retry after %d seconds", e.Method, e.URL, e.RetryAfter)
}
return fmt.Sprintf("GitCode API: rate limited (%s %s)", e.Method, e.URL)
}
// ConflictError represents a 409 Conflict error.
type ConflictError struct {
*APIError
}
func (e *ConflictError) Error() string {
return fmt.Sprintf("GitCode API: conflict (%s %s) - resource already exists", e.Method, e.URL)
}
// ValidationError represents a 422 Unprocessable Entity error.
type ValidationError struct {
*APIError
Errors []FieldError `json:"errors,omitempty"`
}
func (e *ValidationError) Error() string {
if len(e.Errors) > 0 {
return fmt.Sprintf("GitCode API: validation failed (%s %s): %s", e.Method, e.URL, e.Errors[0].Message)
}
return e.APIError.Error()
}
// FieldError represents a field-level validation error.
type FieldError struct {
Resource string `json:"resource"`
Field string `json:"field"`
Code string `json:"code"`
Message string `json:"message,omitempty"`
}
// IsNotFound checks if an error is a NotFoundError.
func IsNotFound(err error) bool {
_, ok := err.(*NotFoundError)
return ok
}
// IsUnauthorized checks if an error is an UnauthorizedError.
func IsUnauthorized(err error) bool {
_, ok := err.(*UnauthorizedError)
return ok
}
// IsRateLimit checks if an error is a RateLimitError.
func IsRateLimit(err error) bool {
_, ok := err.(*RateLimitError)
return ok
}
// IsConflict checks if an error is a ConflictError.
func IsConflict(err error) bool {
_, ok := err.(*ConflictError)
return ok
}
// IsForbidden checks if an error is a ForbiddenError.
func IsForbidden(err error) bool {
_, ok := err.(*ForbiddenError)
return ok
}
// IsValidation checks if an error is a ValidationError.
func IsValidation(err error) bool {
_, ok := err.(*ValidationError)
return ok
}
// newAPIError creates a structured error from an HTTP response.
func newAPIError(method, url string, statusCode int, body string) error {
apiErr := &APIError{
StatusCode: statusCode,
Message: body,
URL: url,
Method: method,
Body: body,
}
switch statusCode {
case http.StatusNotFound:
return &NotFoundError{APIError: apiErr}
case http.StatusUnauthorized:
return &UnauthorizedError{APIError: apiErr}
case http.StatusForbidden:
return &ForbiddenError{APIError: apiErr}
case http.StatusTooManyRequests:
return &RateLimitError{APIError: apiErr}
case http.StatusConflict:
return &ConflictError{APIError: apiErr}
case http.StatusUnprocessableEntity:
return &ValidationError{APIError: apiErr}
default:
return apiErr
}
}