-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
72 lines (63 loc) · 1.96 KB
/
Copy patherrors.go
File metadata and controls
72 lines (63 loc) · 1.96 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
package goconfy
import (
"fmt"
"strings"
)
// FieldError represents an error that occurred during a specific phase of the configuration pipeline.
// It provides contextual information like the field path, line and column numbers,
// and the pipeline layer where the error occurred.
type FieldError struct {
// Field is the name of the struct field associated with the error (if applicable).
Field string
// Message is the human-readable error message.
Message string
// Line is the 1-based line number in the YAML source.
Line int
// Column is the 1-based column number in the YAML source.
Column int
// Layer is the pipeline phase where the error occurred (e.g., "base", "dotenv", "profile", "validation").
Layer string
// Path is the dot-separated path to the configuration key (e.g., "db.password").
Path string
}
// Error implements the error interface, providing a formatted string with all available context.
func (e *FieldError) Error() string {
var sb strings.Builder
if e.Layer != "" {
sb.WriteString(fmt.Sprintf("[%s] ", e.Layer))
}
if e.Path != "" {
sb.WriteString(fmt.Sprintf("path %q: ", e.Path))
} else if e.Field != "" {
sb.WriteString(fmt.Sprintf("field %q: ", e.Field))
}
if e.Line > 0 {
if e.Column > 0 {
sb.WriteString(fmt.Sprintf("line %d, col %d: ", e.Line, e.Column))
} else {
sb.WriteString(fmt.Sprintf("line %d: ", e.Line))
}
}
sb.WriteString(e.Message)
return sb.String()
}
// MultiError collects multiple errors into a single error.
// It implements errors.Join compatibility by providing an Unwrap() []error method.
type MultiError struct {
Errors []error
}
// Error implements the error interface.
func (e *MultiError) Error() string {
if len(e.Errors) == 0 {
return ""
}
msgs := make([]string, 0, len(e.Errors))
for _, err := range e.Errors {
msgs = append(msgs, err.Error())
}
return strings.Join(msgs, "; ")
}
// Unwrap returns the list of wrapped errors.
func (e *MultiError) Unwrap() []error {
return e.Errors
}