-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
81 lines (64 loc) · 2.4 KB
/
Copy patherrors.go
File metadata and controls
81 lines (64 loc) · 2.4 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
package lettermint
import (
"errors"
"fmt"
)
// Sentinel errors for type checking with errors.Is()
var (
// ErrInvalidAPIToken indicates the API token is missing or invalid.
ErrInvalidAPIToken = errors.New("lettermint: invalid or missing API token")
// ErrInvalidRequest indicates request validation failed before sending.
ErrInvalidRequest = errors.New("lettermint: invalid request")
// ErrUnauthorized indicates authentication failed (HTTP 401).
ErrUnauthorized = errors.New("lettermint: unauthorized")
// ErrValidation indicates validation error from API (HTTP 422).
ErrValidation = errors.New("lettermint: validation error")
// ErrRateLimited indicates rate limit exceeded (HTTP 429).
ErrRateLimited = errors.New("lettermint: rate limit exceeded")
// ErrServerError indicates server error (HTTP 5xx).
ErrServerError = errors.New("lettermint: server error")
// ErrTimeout indicates request timeout.
ErrTimeout = errors.New("lettermint: request timeout")
// ErrInvalidWebhookSignature indicates webhook signature verification failed.
ErrInvalidWebhookSignature = errors.New("lettermint: invalid webhook signature")
// ErrWebhookTimestampExpired indicates webhook timestamp is outside tolerance window.
ErrWebhookTimestampExpired = errors.New("lettermint: webhook timestamp outside tolerance window")
)
// APIError represents an error response from the Lettermint API.
type APIError struct {
// StatusCode is the HTTP status code returned by the API.
StatusCode int
// Message is the error message from the API.
Message string
// ErrorType is the specific error type (e.g., "validation_error").
ErrorType string
// Errors contains field-specific validation errors.
Errors map[string][]string
// ResponseBody is the raw response body for debugging.
ResponseBody string
}
// Error implements the error interface.
func (e *APIError) Error() string {
if e.ErrorType != "" {
return fmt.Sprintf("lettermint: API error (%d): %s [%s]", e.StatusCode, e.Message, e.ErrorType)
}
return fmt.Sprintf("lettermint: API error (%d): %s", e.StatusCode, e.Message)
}
// Unwrap returns the underlying sentinel error for use with errors.Is().
func (e *APIError) Unwrap() error {
switch e.StatusCode {
case 400:
return ErrInvalidRequest
case 401:
return ErrUnauthorized
case 422:
return ErrValidation
case 429:
return ErrRateLimited
default:
if e.StatusCode >= 500 {
return ErrServerError
}
return nil
}
}