Skip to content

Commit 3afa723

Browse files
authored
fix: improve errors (#329)
* fix: improve errors closes #328 Signed-off-by: Carlos Alexandro Becker <[email protected]> * fix: rename func --------- Signed-off-by: Carlos Alexandro Becker <[email protected]>
1 parent 0136931 commit 3afa723

File tree

4 files changed

+43
-40
lines changed

4 files changed

+43
-40
lines changed

env.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,11 @@ func get(fieldParams FieldParams, opts Options) (val string, err error) {
563563
}
564564

565565
if fieldParams.Required && !exists && len(fieldParams.OwnKey) > 0 {
566-
return "", newEnvVarIsNotSet(fieldParams.Key)
566+
return "", newVarIsNotSetError(fieldParams.Key)
567567
}
568568

569569
if fieldParams.NotEmpty && val == "" {
570-
return "", newEmptyEnvVarError(fieldParams.Key)
570+
return "", newEmptyVarError(fieldParams.Key)
571571
}
572572

573573
if fieldParams.LoadFile && val != "" {

env_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,8 @@ func TestParsesEnvInnerFailsMultipleErrors(t *testing.T) {
650650
err := Parse(&config{})
651651
isErrorWithMessage(t, err, `env: required environment variable "NAME" is not set; parse error on field "Number" of type "int": strconv.ParseInt: parsing "not-a-number": invalid syntax; required environment variable "AGE" is not set`)
652652
isTrue(t, errors.Is(err, ParseError{}))
653-
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
654-
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
653+
isTrue(t, errors.Is(err, VarIsNotSetError{}))
654+
isTrue(t, errors.Is(err, VarIsNotSetError{}))
655655
}
656656

657657
func TestParsesEnvInnerNil(t *testing.T) {
@@ -932,7 +932,7 @@ func TestErrorRequiredNotSet(t *testing.T) {
932932
}
933933
err := Parse(&config{})
934934
isErrorWithMessage(t, err, `env: required environment variable "IS_REQUIRED" is not set`)
935-
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
935+
isTrue(t, errors.Is(err, VarIsNotSetError{}))
936936
}
937937

938938
func TestNoErrorNotEmptySet(t *testing.T) {
@@ -958,7 +958,7 @@ func TestErrorNotEmptySet(t *testing.T) {
958958
}
959959
err := Parse(&config{})
960960
isErrorWithMessage(t, err, `env: environment variable "IS_REQUIRED" should not be empty`)
961-
isTrue(t, errors.Is(err, EmptyEnvVarError{}))
961+
isTrue(t, errors.Is(err, EmptyVarError{}))
962962
}
963963

964964
func TestErrorRequiredAndNotEmptySet(t *testing.T) {
@@ -968,7 +968,7 @@ func TestErrorRequiredAndNotEmptySet(t *testing.T) {
968968
}
969969
err := Parse(&config{})
970970
isErrorWithMessage(t, err, `env: environment variable "IS_REQUIRED" should not be empty`)
971-
isTrue(t, errors.Is(err, EmptyEnvVarError{}))
971+
isTrue(t, errors.Is(err, EmptyVarError{}))
972972
}
973973

974974
func TestErrorRequiredNotSetWithDefault(t *testing.T) {
@@ -1048,7 +1048,7 @@ func TestParseUnsetRequireOptions(t *testing.T) {
10481048

10491049
err := Parse(&cfg)
10501050
isErrorWithMessage(t, err, `env: required environment variable "PASSWORD" is not set`)
1051-
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
1051+
isTrue(t, errors.Is(err, VarIsNotSetError{}))
10521052
t.Setenv("PASSWORD", "superSecret")
10531053
isNoErr(t, Parse(&cfg))
10541054

@@ -1467,7 +1467,7 @@ func TestFileNoParamRequired(t *testing.T) {
14671467

14681468
err := Parse(&config{})
14691469
isErrorWithMessage(t, err, `env: required environment variable "SECRET_KEY" is not set`)
1470-
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
1470+
isTrue(t, errors.Is(err, VarIsNotSetError{}))
14711471
}
14721472

14731473
func TestFileBadFile(t *testing.T) {
@@ -1557,11 +1557,11 @@ func TestRequiredIfNoDefOption(t *testing.T) {
15571557
t.Run("missing", func(t *testing.T) {
15581558
err := ParseWithOptions(&cfg, Options{RequiredIfNoDef: true})
15591559
isErrorWithMessage(t, err, `env: required environment variable "NAME" is not set; required environment variable "FRUIT" is not set`)
1560-
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
1560+
isTrue(t, errors.Is(err, VarIsNotSetError{}))
15611561
t.Setenv("NAME", "John")
15621562
err = ParseWithOptions(&cfg, Options{RequiredIfNoDef: true})
15631563
isErrorWithMessage(t, err, `env: required environment variable "FRUIT" is not set`)
1564-
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
1564+
isTrue(t, errors.Is(err, VarIsNotSetError{}))
15651565
})
15661566

15671567
t.Run("all set", func(t *testing.T) {
@@ -1593,7 +1593,7 @@ func TestRequiredIfNoDefNested(t *testing.T) {
15931593

15941594
err := ParseWithOptions(&cfg, Options{RequiredIfNoDef: true})
15951595
isErrorWithMessage(t, err, `env: required environment variable "SERVER_PORT" is not set`)
1596-
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
1596+
isTrue(t, errors.Is(err, VarIsNotSetError{}))
15971597
})
15981598

15991599
t.Run("all set", func(t *testing.T) {
@@ -2155,7 +2155,7 @@ func TestIssue298ErrorNestedFieldRequiredNotSet(t *testing.T) {
21552155
cfg := ComplexConfig{}
21562156
err := Parse(&cfg)
21572157
isErrorWithMessage(t, err, `env: required environment variable "FOO_0_STR" is not set`)
2158-
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
2158+
isTrue(t, errors.Is(err, VarIsNotSetError{}))
21592159
}
21602160

21612161
func TestIssue320(t *testing.T) {

error.go

+28-25
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
// NotStructPtrError
1313
// NoParserError
1414
// NoSupportedTagOptionError
15-
// EnvVarIsNotSetError
16-
// EmptyEnvVarError
15+
// VarIsNotSetError
16+
// EmptyVarError
1717
// LoadFileContentError
1818
// ParseValueError
1919
type AggregateError struct {
@@ -65,16 +65,14 @@ func (e ParseError) Error() string {
6565
return fmt.Sprintf(`parse error on field "%s" of type "%s": %v`, e.Name, e.Type, e.Err)
6666
}
6767

68-
// The error occurs when pass something that is not a pointer to a Struct to Parse
68+
// The error occurs when pass something that is not a pointer to a struct to Parse
6969
type NotStructPtrError struct{}
7070

7171
func (e NotStructPtrError) Error() string {
7272
return "expected a pointer to a Struct"
7373
}
7474

75-
// This error occurs when there is no parser provided for given type
76-
// Supported types and defaults: https://github.com/caarlos0/env#supported-types-and-defaults
77-
// How to create a custom parser: https://github.com/caarlos0/env#custom-parser-funcs
75+
// This error occurs when there is no parser provided for given type.
7876
type NoParserError struct {
7977
Name string
8078
Type reflect.Type
@@ -88,9 +86,9 @@ func (e NoParserError) Error() string {
8886
return fmt.Sprintf(`no parser found for field "%s" of type "%s"`, e.Name, e.Type)
8987
}
9088

91-
// This error occurs when the given tag is not supported
92-
// In-built supported tags: "", "file", "required", "unset", "notEmpty", "expand", "envDefault", "envSeparator"
93-
// How to create a custom tag: https://github.com/caarlos0/env#changing-default-tag-name
89+
// This error occurs when the given tag is not supported.
90+
// Built-in supported tags: "", "file", "required", "unset", "notEmpty",
91+
// "expand", "envDefault", and "envSeparator".
9492
type NoSupportedTagOptionError struct {
9593
Tag string
9694
}
@@ -103,36 +101,43 @@ func (e NoSupportedTagOptionError) Error() string {
103101
return fmt.Sprintf("tag option %q not supported", e.Tag)
104102
}
105103

106-
// This error occurs when the required variable is not set
107-
// Read about required fields: https://github.com/caarlos0/env#required-fields
108-
type EnvVarIsNotSetError struct {
104+
// This error occurs when the required variable is not set.
105+
//
106+
// deprecated: use VarIsNotSetError
107+
type EnvVarIsNotSetError = VarIsNotSetError
108+
109+
// This error occurs when the required variable is not set.
110+
type VarIsNotSetError struct {
109111
Key string
110112
}
111113

112-
func newEnvVarIsNotSet(key string) error {
113-
return EnvVarIsNotSetError{key}
114+
func newVarIsNotSetError(key string) error {
115+
return VarIsNotSetError{key}
114116
}
115117

116-
func (e EnvVarIsNotSetError) Error() string {
118+
func (e VarIsNotSetError) Error() string {
117119
return fmt.Sprintf(`required environment variable %q is not set`, e.Key)
118120
}
119121

120122
// This error occurs when the variable which must be not empty is existing but has an empty value
121-
// Read about not empty fields: https://github.com/caarlos0/env#not-empty-fields
122-
type EmptyEnvVarError struct {
123+
//
124+
// deprecated: use EmptyVarError
125+
type EmptyEnvVarError = EmptyVarError
126+
127+
// This error occurs when the variable which must be not empty is existing but has an empty value
128+
type EmptyVarError struct {
123129
Key string
124130
}
125131

126-
func newEmptyEnvVarError(key string) error {
127-
return EmptyEnvVarError{key}
132+
func newEmptyVarError(key string) error {
133+
return EmptyVarError{key}
128134
}
129135

130-
func (e EmptyEnvVarError) Error() string {
136+
func (e EmptyVarError) Error() string {
131137
return fmt.Sprintf("environment variable %q should not be empty", e.Key)
132138
}
133139

134-
// This error occurs when it's impossible to load the value from the file
135-
// Read about From file feature: https://github.com/caarlos0/env#from-file
140+
// This error occurs when it's impossible to load the value from the file.
136141
type LoadFileContentError struct {
137142
Filename string
138143
Key string
@@ -147,9 +152,7 @@ func (e LoadFileContentError) Error() string {
147152
return fmt.Sprintf(`could not load content of file "%s" from variable %s: %v`, e.Filename, e.Key, e.Err)
148153
}
149154

150-
// This error occurs when it's impossible to convert value using given parser
151-
// Supported types and defaults: https://github.com/caarlos0/env#supported-types-and-defaults
152-
// How to create a custom parser: https://github.com/caarlos0/env#custom-parser-funcs
155+
// This error occurs when it's impossible to convert value using given parser.
153156
type ParseValueError struct {
154157
Msg string
155158
Err error

example_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ func ExampleParse_errorHandling() {
424424

425425
var cfg Config
426426
if err := Parse(&cfg); err != nil {
427-
if errors.Is(err, EmptyEnvVarError{}) {
427+
if errors.Is(err, EmptyVarError{}) {
428428
fmt.Println("oopsie")
429429
}
430430
aggErr := AggregateError{}
@@ -440,7 +440,7 @@ func ExampleParse_errorHandling() {
440440
// EmptyEnvVarError
441441
// LoadFileContentError
442442
// ParseValueError
443-
case EmptyEnvVarError:
443+
case EmptyVarError:
444444
fmt.Println("daisy")
445445
default:
446446
fmt.Printf("Unknown error type %v", v)

0 commit comments

Comments
 (0)