-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
56 lines (47 loc) · 1.57 KB
/
error.go
File metadata and controls
56 lines (47 loc) · 1.57 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
package env
import (
"fmt"
"strings"
)
type ErrorType string
func (e ErrorType) Error() string {
return string(e)
}
const (
ErrorRequired ErrorType = "required"
ErrorWrongType ErrorType = "wrong_type"
ErrorParserMissingType ErrorType = "parser_missing"
ErrorPointerSetterMissing ErrorType = "pointer_setter_missing"
)
type ErrorCollection struct {
Errors []FieldError
}
func (collection *ErrorCollection) Error() string {
msgParts := []string{}
for _, err := range collection.Errors {
msgParts = append(msgParts, err.Error())
}
return strings.Join(msgParts, "\n")
}
type FieldError struct {
Location string
ErrorType ErrorType
VariableType string
OriginalError error
}
func (fieldError *FieldError) Error() string {
createErrorMsg := errorMsgGenerators[fieldError.ErrorType]
return createErrorMsg(fieldError)
}
var errorMsgGenerators = map[ErrorType]func(*FieldError) string{
ErrorRequired: func(fe *FieldError) string { return fmt.Sprintf("Environmental variable '%s' is unset", fe.Location) },
ErrorWrongType: func(fe *FieldError) string {
return fmt.Sprintf("Environmental variable '%s' has wrong type. Required type: '%s'", fe.Location, fe.VariableType)
},
ErrorParserMissingType: func(fe *FieldError) string {
return fmt.Sprintf("Parser missing for environmental variable '%s'. Required type: '%s'", fe.Location, fe.VariableType)
},
ErrorPointerSetterMissing: func(fe *FieldError) string {
return fmt.Sprintf("Pointer setter missing for environmental variable '%s'. Required type: '%s'", fe.Location, fe.VariableType)
},
}