-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
75 lines (59 loc) · 2 KB
/
errors.go
File metadata and controls
75 lines (59 loc) · 2 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
package mailpitclient
import "fmt"
// ErrorType represents the type of error that occurred.
type ErrorType string
const (
// ErrorTypeConfig indicates a configuration error
ErrorTypeConfig ErrorType = "config"
// ErrorTypeNetwork indicates a network-related error
ErrorTypeNetwork ErrorType = "network"
// ErrorTypeRequest indicates an error creating the HTTP request
ErrorTypeRequest ErrorType = "request"
// ErrorTypeResponse indicates an error parsing the HTTP response
ErrorTypeResponse ErrorType = "response"
// ErrorTypeAPI indicates an API error returned by the server
ErrorTypeAPI ErrorType = "api"
// ErrorTypeValidation indicates a validation error
ErrorTypeValidation ErrorType = "validation"
)
// Error represents a Mailpit client error with structured information.
type Error struct {
Cause error `json:"-"`
Type ErrorType `json:"type"`
Message string `json:"message"`
Response string `json:"response,omitempty"`
StatusCode int `json:"status_code,omitempty"`
}
// Error implements the error interface.
func (e *Error) Error() string {
if e.StatusCode > 0 {
return fmt.Sprintf("mailpit %s error (status %d): %s", e.Type, e.StatusCode, e.Message)
}
return fmt.Sprintf("mailpit %s error: %s", e.Type, e.Message)
}
// Unwrap returns the underlying error.
func (e *Error) Unwrap() error {
return e.Cause
}
// IsType checks if the error is of a specific type.
func (e *Error) IsType(errorType ErrorType) bool {
return e.Type == errorType
}
// IsAPIError checks if the error is an API error with the given status code.
func (e *Error) IsAPIError(statusCode int) bool {
return e.Type == ErrorTypeAPI && e.StatusCode == statusCode
}
// NewConfigError creates a new configuration error.
func NewConfigError(message string) *Error {
return &Error{
Type: ErrorTypeConfig,
Message: message,
}
}
// NewValidationError creates a new validation error.
func NewValidationError(message string) *Error {
return &Error{
Type: ErrorTypeValidation,
Message: message,
}
}