-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
69 lines (57 loc) · 1.27 KB
/
errors.go
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
package config
import (
"fmt"
)
// Errors is a map of field name to Error.
type Errors struct {
errors map[string]*Error
}
func (e *Errors) Add(err *Error) *Errors {
if e.errors == nil {
e.errors = make(map[string]*Error)
}
e.errors[err.Field] = err
return e
}
func (e *Errors) HasField(fieldName string) bool {
if e.errors == nil {
return false
}
_, ok := e.errors[fieldName]
return ok
}
func (e *Errors) Error() string {
var fieldNames []string
for _, err := range e.errors {
fieldNames = append(fieldNames, err.Field)
}
return fmt.Sprintf("invalid config for fields: %v", fieldNames)
}
func (e *Errors) AsError() error {
if e == nil || len(e.errors) == 0 {
return nil
}
return e
}
type Error struct {
Field string
Value interface{}
Message string
Inner error
}
func Err(field string, val interface{}, message string) *Error {
return &Error{Field: field, Value: val, Message: message}
}
func (e *Error) WithInner(err error) *Error {
e.Inner = err
return e
}
func EmptyErr(field string, val string) *Error {
return &Error{Field: field, Value: val, Message: "must not be empty"}
}
func (e *Error) Error() string {
return fmt.Sprintf("field %s (with val: %s): %s", e.Field, e.Value, e.Message)
}
func (e *Error) Unwrap() error {
return e.Inner
}