-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patherrors.go
More file actions
201 lines (167 loc) · 5.15 KB
/
Copy patherrors.go
File metadata and controls
201 lines (167 loc) · 5.15 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Package internal provides error types and helpers for github-copilot-svcs.
package internal
import (
"fmt"
"net/http"
)
type (
// AuthenticationError ...
AuthenticationError struct {
Message string
Err error
}
// ConfigurationError ...
ConfigurationError struct {
Field string
Value interface{}
Message string
Err error
}
// NetworkError ...
NetworkError struct {
Operation string
URL string
Message string
Err error
}
// ValidationError ...
ValidationError struct {
Field string
Value interface{}
Message string
Err error
}
// ProxyError ...
ProxyError struct {
Operation string
Message string
Err error
}
)
func (e *AuthenticationError) Error() string {
if e.Err != nil {
return fmt.Sprintf("authentication error: %s: %v", e.Message, e.Err)
}
return fmt.Sprintf("authentication error: %s", e.Message)
}
func (e *AuthenticationError) Unwrap() error {
return e.Err
}
func (e *ConfigurationError) Error() string {
if e.Err != nil {
return fmt.Sprintf("configuration error for %s=%v: %s: %v", e.Field, e.Value, e.Message, e.Err)
}
return fmt.Sprintf("configuration error for %s=%v: %s", e.Field, e.Value, e.Message)
}
func (e *ConfigurationError) Unwrap() error {
return e.Err
}
func (e *NetworkError) Error() string {
if e.Err != nil {
return fmt.Sprintf("network error during %s to %s: %s: %v", e.Operation, e.URL, e.Message, e.Err)
}
return fmt.Sprintf("network error during %s to %s: %s", e.Operation, e.URL, e.Message)
}
func (e *NetworkError) Unwrap() error {
return e.Err
}
func (e *ValidationError) Error() string {
if e.Err != nil {
return fmt.Sprintf("validation error for %s=%v: %s: %v", e.Field, e.Value, e.Message, e.Err)
}
return fmt.Sprintf("validation error for %s=%v: %s", e.Field, e.Value, e.Message)
}
func (e *ValidationError) Unwrap() error {
return e.Err
}
func (e *ProxyError) Error() string {
if e.Err != nil {
return fmt.Sprintf("proxy error during %s: %s: %v", e.Operation, e.Message, e.Err)
}
return fmt.Sprintf("proxy error during %s: %s", e.Operation, e.Message)
}
func (e *ProxyError) Unwrap() error {
return e.Err
}
// NewAuthError ...
func NewAuthError(message string, err error) *AuthenticationError {
return &AuthenticationError{Message: message, Err: err}
}
// NewConfigError ...
func NewConfigError(field string, value interface{}, message string, err error) *ConfigurationError {
return &ConfigurationError{Field: field, Value: value, Message: message, Err: err}
}
// NewNetworkError ...
func NewNetworkError(operation, url, message string, err error) *NetworkError {
return &NetworkError{Operation: operation, URL: url, Message: message, Err: err}
}
// NewValidationError ...
func NewValidationError(field string, value interface{}, message string, err error) *ValidationError {
return &ValidationError{Field: field, Value: value, Message: message, Err: err}
}
// NewProxyError ...
func NewProxyError(operation, message string, err error) *ProxyError {
return &ProxyError{Operation: operation, Message: message, Err: err}
}
// WriteHTTPError ...
func WriteHTTPError(w http.ResponseWriter, statusCode int, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
_, _ = fmt.Fprintf(w, `{"error": {"message": "%s", "type": "error", "code": %d}}`, message, statusCode)
}
// WriteHTTPErrorWithDetails ...
func WriteHTTPErrorWithDetails(w http.ResponseWriter, statusCode int, errorType, message, details string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
_, _ = fmt.Fprintf(w, `{"error": {"message": "%s", "type": "%s", "code": %d, "details": "%s"}}`,
message, errorType, statusCode, details)
}
// WriteAuthenticationError ...
func WriteAuthenticationError(w http.ResponseWriter) {
WriteHTTPError(w, http.StatusUnauthorized, "Authentication required")
}
// WriteAuthorizationError ...
func WriteAuthorizationError(w http.ResponseWriter) {
WriteHTTPError(w, http.StatusForbidden, "Insufficient permissions")
}
// WriteValidationError ...
func WriteValidationError(w http.ResponseWriter, message string) {
WriteHTTPError(w, http.StatusBadRequest, message)
}
// WriteInternalError ...
func WriteInternalError(w http.ResponseWriter) {
WriteHTTPError(w, http.StatusInternalServerError, "Internal server error")
}
// WriteServiceUnavailableError ...
func WriteServiceUnavailableError(w http.ResponseWriter) {
WriteHTTPError(w, http.StatusServiceUnavailable, "Service temporarily unavailable")
}
// WriteRateLimitError ...
func WriteRateLimitError(w http.ResponseWriter) {
WriteHTTPError(w, http.StatusTooManyRequests, "Rate limit exceeded")
}
// IsAuthenticationError ...
func IsAuthenticationError(err error) bool {
_, ok := err.(*AuthenticationError)
return ok
}
// IsConfigurationError ...
func IsConfigurationError(err error) bool {
_, ok := err.(*ConfigurationError)
return ok
}
// IsNetworkError ...
func IsNetworkError(err error) bool {
_, ok := err.(*NetworkError)
return ok
}
// IsValidationError ...
func IsValidationError(err error) bool {
_, ok := err.(*ValidationError)
return ok
}
// IsProxyError ...
func IsProxyError(err error) bool {
_, ok := err.(*ProxyError)
return ok
}