-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
83 lines (71 loc) · 2.84 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package settings
import (
"fmt"
"reflect"
)
// SettingsError is any type of error that is raised specifically related to gathering
// and applying settings from each of the specified sources
type SettingsError struct {
Message string
}
func (e SettingsError) Error() string {
return e.Message
}
// SettingsFieldDoesNotExist is an error when a field is specified via the DefaultsMap that does not exist
// in the out struct value that is provided to settings.Gather
func SettingsFieldDoesNotExist(overrideType string, fieldName string) SettingsError {
return SettingsError{
Message: fmt.Sprintf("field specified in override (%s) does not exist in the target out struct: %s", overrideType, fieldName),
}
}
// SettingsFieldTypeMismatch is raised in the event there is a mismatch between types when trying to override a specific value
func SettingsFieldTypeMismatch(fieldName string, expectedType reflect.Kind, receivedType reflect.Kind) SettingsError {
return SettingsError{
Message: fmt.Sprintf("type mismatch for field %s: expected %v but value is %v", fieldName, expectedType, receivedType),
}
}
// SettingsFieldSetError is raised when a field's value is not actually settable
func SettingsFieldSetError(fieldName string, t reflect.Kind, m ...error) SettingsError {
if len(m) == 0 {
return SettingsError{
Message: fmt.Sprintf("unable to set the value of a field in settings: %s (type: %v)", fieldName, t),
}
}
return SettingsError{
Message: fmt.Sprintf(
"unable to set the value of a field in settings: %s (type: %v): %s",
fieldName,
t,
m[0].Error()),
}
}
// SettingsFileParseError occurs when a specified settings file can't be properly unmarshalled
func SettingsFileParseError(path string, desc string) SettingsError {
return SettingsError{
Message: fmt.Sprintf("unable to parse settings file (%s): %s", path, desc),
}
}
// SettingsFileReadError occurs when a specified settings file is not readable
func SettingsFileReadError(path string, desc string) SettingsError {
return SettingsError{
Message: fmt.Sprintf("unable to read settings file (%s): %s", path, desc),
}
}
// SettingsFileTypeError occurs when a format is requested that the settings package does not support
func SettingsFileTypeError(path string, ext string) SettingsError {
return SettingsError{
Message: fmt.Sprintf("unrecognized settings file extension (%s): %s", path, ext),
}
}
// SettingsOutCannotBeNil occurs when the out field in the settings struct is set to nil, intentionally or otherwise
func SettingsOutCannotBeNil() SettingsError {
return SettingsError{
Message: "out cannot be nil",
}
}
// SettingsTypeDiscoveryError occurs when the out value provided to settings.Gather is not a struct
func SettingsTypeDiscoveryError(t reflect.Kind) SettingsError {
return SettingsError{
Message: fmt.Sprintf("unable to detect fields for non-struct type: %v", t),
}
}