-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
47 lines (39 loc) · 1.33 KB
/
error.go
File metadata and controls
47 lines (39 loc) · 1.33 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
package goenvconf
import "fmt"
var (
// ErrEnvironmentValueRequired occurs when both value and env fields are null or empty.
ErrEnvironmentValueRequired = ParseEnvError{
Code: "EmptyEnv",
Detail: "require either value or env",
}
// ErrEnvironmentVariableValueRequired the error that occurs when the value from environment variable is empty.
ErrEnvironmentVariableValueRequired = ParseEnvError{
Code: "EmptyVar",
Detail: "the environment variable value is empty",
}
)
const (
// ErrCodeParseEnvFailed is the error code when parsing environment variable failed.
ErrCodeParseEnvFailed = "ParseEnvFailed"
)
// ParseEnvError structures a detailed error for parsed env.
type ParseEnvError struct {
Code string `json:"code" jsonschema:"enum=EmptyEnv,enum=EmptyVar,enum=ParseEnvFailed"`
Detail string `json:"detail"`
Hint string `json:"hint,omitempty"`
}
// NewParseEnvFailedError creates a [ParseEnvError] for parsing env variable errors.
func NewParseEnvFailedError(detail string, hint string) ParseEnvError {
return ParseEnvError{
Code: ErrCodeParseEnvFailed,
Detail: detail,
Hint: hint,
}
}
// Error returns the error message.
func (pee ParseEnvError) Error() string {
if pee.Hint != "" {
return fmt.Sprintf("%s: %s. Hint: %s", pee.Code, pee.Detail, pee.Hint)
}
return pee.Code + ": " + pee.Detail
}