-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.go
More file actions
29 lines (23 loc) · 715 Bytes
/
field.go
File metadata and controls
29 lines (23 loc) · 715 Bytes
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
package errpath
import "strings"
var _ pathWriter = (*ErrField)(nil)
// ErrField is an error that occurred in a field.
type ErrField struct {
// The name of the field where the error occurred.
Field string
// The underlying error.
Err error
}
// Error returns the whole path to the field and the error message.
func (e *ErrField) Error() string {
b := &strings.Builder{}
b.WriteString(e.Field)
return writePath(b, e.Err)
}
// writePath appends the field to the path by separating it with a dot from the previous path
func (e *ErrField) writePath(b *strings.Builder) {
b.WriteString(".")
b.WriteString(e.Field)
}
// Unwrap returns the wrapped error.
func (e *ErrField) Unwrap() error { return e.Err }