-
Notifications
You must be signed in to change notification settings - Fork 5
/
error.go
71 lines (59 loc) · 1.7 KB
/
error.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
70
71
package envvar
import (
"fmt"
"strings"
)
// UnsetVariableError is returned by Parse whenever a required environment
// variable is not set.
type UnsetVariableError struct {
// VarName is the name of the required environment variable that was not set
VarName string
}
// InvalidFieldError is returned by Parse whenever a given struct field
// is unsupported.
type InvalidFieldError struct {
Name string
Message string
}
// InvalidVariableError is returned when a given env var cannot be parsed to
// a given struct field.
type InvalidVariableError struct {
VarName string
VarValue string
parent error // optional
}
// InvalidArgumentError is raised when an invalid argument passed.
type InvalidArgumentError struct {
message string
}
// ErrorList is list of independent errors raised by Parse
type ErrorList struct {
Errors []error
}
func (e InvalidArgumentError) Error() string {
return "envvar: " + e.message
}
// Error satisfies the error interface
func (e UnsetVariableError) Error() string {
return fmt.Sprintf("Missing required environment variable: %s", e.VarName)
}
// Error satisfies the error interface
func (e InvalidVariableError) Error() string {
return fmt.Sprintf("Error parsing environment variable %s: %s (%s)", e.VarName, e.VarValue, errorOrUnknown(e.parent))
}
func (e InvalidFieldError) Error() string {
return fmt.Sprintf("Unsupported struct field %s: %s", e.Name, e.Message)
}
func errorOrUnknown(err error) string {
if err != nil {
return err.Error()
}
return "unknown"
}
func (e ErrorList) Error() string {
allErrors := []string{}
for _, err := range e.Errors {
allErrors = append(allErrors, "envvar: "+err.Error())
}
return fmt.Sprintf(strings.Join(allErrors, "\n"))
}