-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: include more context in errors (#31)
* feat: include more context in errors * chore: make receivers as non-pointer type for errors
- Loading branch information
1 parent
0020145
commit 6f7f235
Showing
7 changed files
with
154 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package xload | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// ErrRequired is returned when a required key is missing. | ||
type ErrRequired struct{ key string } | ||
|
||
func (e ErrRequired) Error() string { return "required key missing: " + e.key } | ||
|
||
// ErrUnknownTagOption is returned when an unknown tag option is used. | ||
type ErrUnknownTagOption struct { | ||
key string | ||
opt string | ||
} | ||
|
||
func (e ErrUnknownTagOption) Error() string { | ||
if e.key == "" { | ||
return fmt.Sprintf("unknown tag option: %s", e.opt) | ||
} | ||
|
||
return fmt.Sprintf("`%s` key has unknown tag option: %s", e.key, e.opt) | ||
} | ||
|
||
// ErrUnknownFieldType is returned when the key type is not supported. | ||
type ErrUnknownFieldType struct { | ||
field string | ||
kind reflect.Kind | ||
key string | ||
} | ||
|
||
func (e ErrUnknownFieldType) Error() string { | ||
return fmt.Sprintf("`%s: %s` key=%s has an invalid value", e.field, e.kind, e.key) | ||
} | ||
|
||
// ErrInvalidMapValue is returned when the map value is invalid. | ||
type ErrInvalidMapValue struct{ key string } | ||
|
||
func (e ErrInvalidMapValue) Error() string { | ||
return fmt.Sprintf("`%s` key has an invalid map value", e.key) | ||
} | ||
|
||
// ErrInvalidPrefix is returned when the prefix option is used on a non-struct key. | ||
type ErrInvalidPrefix struct { | ||
field string | ||
kind reflect.Kind | ||
} | ||
|
||
func (e ErrInvalidPrefix) Error() string { | ||
return fmt.Sprintf("prefix is only valid on struct types, found `%s: %s`", e.field, e.kind) | ||
} | ||
|
||
// ErrInvalidPrefixAndKey is returned when the prefix option is used with a key. | ||
type ErrInvalidPrefixAndKey struct { | ||
field string | ||
key string | ||
} | ||
|
||
func (e ErrInvalidPrefixAndKey) Error() string { | ||
return fmt.Sprintf("`%s` key=%s has both prefix and key", e.field, e.key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package xload | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestErrUnknownTagOption_Error(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
key string | ||
opt string | ||
want string | ||
}{ | ||
{ | ||
name: "key and opt", | ||
key: "key", | ||
opt: "opt", | ||
want: "`key` key has unknown tag option: opt", | ||
}, | ||
{ | ||
name: "opt only", | ||
opt: "opt", | ||
want: "unknown tag option: opt", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
e := &ErrUnknownTagOption{ | ||
key: tt.key, | ||
opt: tt.opt, | ||
} | ||
assert.Equalf(t, tt.want, e.Error(), "Error()") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.