-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
111 lines (97 loc) · 2.79 KB
/
errors.go
File metadata and controls
111 lines (97 loc) · 2.79 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
package deploykit
import (
"fmt"
"strings"
)
// Error codes returned by the application.
const (
ECONFLICT = "conflict"
EFORBIDDEN = "forbidden"
EINTERNAL = "internal"
EINVALID = "invalid"
ENOTFOUND = "not_found"
EUNAUTHORIZED = "unauthorized"
)
// Error represents a domain-level error with an application error code
// and a human-readable message.
type Error struct {
Code string `json:"error"`
Message string `json:"message"`
}
func (e *Error) Error() string {
return fmt.Sprintf("%s: %s", e.Code, e.Message)
}
// Errorf creates a new Error with the given code and formatted message.
func Errorf(code string, format string, args ...any) *Error {
return &Error{
Code: code,
Message: fmt.Sprintf(format, args...),
}
}
// ValidationErrors is a builder for collecting field-level validation errors.
type ValidationErrors map[string][]string
// NewValidationErrors creates an empty ValidationErrors builder.
func NewValidationErrors() ValidationErrors {
return make(ValidationErrors)
}
// Add appends a validation message for the given field.
func (ve ValidationErrors) Add(field, message string) {
ve[field] = append(ve[field], message)
}
// HasErrors returns true if any field errors have been collected.
func (ve ValidationErrors) HasErrors() bool {
return len(ve) > 0
}
// Err returns a *ValidationError if any errors were collected, or nil otherwise.
// Use this as the return value from Validate() methods.
func (ve ValidationErrors) Err() error {
if !ve.HasErrors() {
return nil
}
return &ValidationError{
Code: EINVALID,
Message: "Validation failed.",
Errors: map[string][]string(ve),
}
}
// ValidationError represents a structured validation failure with per-field error messages.
type ValidationError struct {
Code string `json:"error"`
Message string `json:"message"`
Errors map[string][]string `json:"errors"`
}
func (e *ValidationError) Error() string {
fields := make([]string, 0, len(e.Errors))
for field, msgs := range e.Errors {
fields = append(fields, fmt.Sprintf("%s: %s", field, strings.Join(msgs, "; ")))
}
return fmt.Sprintf("%s: %s (%s)", e.Code, e.Message, strings.Join(fields, ", "))
}
// ErrorCode unwraps an error and returns its code.
// Non-application errors return EINTERNAL.
func ErrorCode(err error) string {
if err == nil {
return ""
}
if e, ok := err.(*Error); ok {
return e.Code
}
if e, ok := err.(*ValidationError); ok {
return e.Code
}
return EINTERNAL
}
// ErrorMessage unwraps an error and returns its human-readable message.
// Non-application errors return "Internal error."
func ErrorMessage(err error) string {
if err == nil {
return ""
}
if e, ok := err.(*Error); ok {
return e.Message
}
if e, ok := err.(*ValidationError); ok {
return e.Message
}
return "Internal error."
}