-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
69 lines (57 loc) · 1.87 KB
/
errors.go
File metadata and controls
69 lines (57 loc) · 1.87 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
package rdapapi
// APIError is the base error type for all RDAP API errors.
type APIError struct {
StatusCode int
Code string
Message string
RetryAfter int
}
// Error implements the error interface.
func (e *APIError) Error() string {
return e.Message
}
// Unwrap returns nil (APIError is the root error type).
func (e *APIError) Unwrap() error {
return nil
}
// ValidationError is returned when the input is invalid (HTTP 400).
type ValidationError struct{ *APIError }
// AuthenticationError is returned when the API key is missing or invalid (HTTP 401).
type AuthenticationError struct{ *APIError }
// SubscriptionRequiredError is returned when no active subscription exists (HTTP 403).
type SubscriptionRequiredError struct{ *APIError }
// NotFoundError is returned when no RDAP data is found for the query (HTTP 404).
type NotFoundError struct{ *APIError }
// RateLimitError is returned when rate limit or monthly quota is exceeded (HTTP 429).
type RateLimitError struct{ *APIError }
// TemporarilyUnavailableError is returned when the domain data is temporarily unavailable (HTTP 503).
type TemporarilyUnavailableError struct{ *APIError }
// UpstreamError is returned when the upstream RDAP server fails (HTTP 502).
type UpstreamError struct{ *APIError }
// newError creates a typed error based on the HTTP status code.
func newError(statusCode int, code, message string, retryAfter int) error {
base := &APIError{
StatusCode: statusCode,
Code: code,
Message: message,
RetryAfter: retryAfter,
}
switch statusCode {
case 400:
return &ValidationError{base}
case 401:
return &AuthenticationError{base}
case 403:
return &SubscriptionRequiredError{base}
case 404:
return &NotFoundError{base}
case 429:
return &RateLimitError{base}
case 502:
return &UpstreamError{base}
case 503:
return &TemporarilyUnavailableError{base}
default:
return base
}
}