8000 Merge pull request #105 from go-viper/error-refactor · go-viper/mapstructure@36de1e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 36de1e1

Browse files
Merge pull request #105 from go-viper/error-refactor
feat: add common error interface
2 parents 599cb73 + 6a283a3 commit 36de1e1

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

errors.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import (
55
"reflect"
66
)
77

8+
// Error interface is implemented by all errors emitted by mapstructure.
9+
//
10+
// Use [errors.As] to check if an error implements this interface.
11+
type Error interface {
12+
error
13+
14+
mapstructure()
15+
}
16+
817
// DecodeError is a generic error type that holds information about
918
// a decoding error together with the name of the field that caused the error.
1019
type DecodeError struct {
@@ -31,27 +40,35 @@ func (e *DecodeError) Error() string {
3140
return fmt.Sprintf("'%s' %s", e.name, e.err)
3241
}
3342

43+
func (*DecodeError) mapstructure() {}
44+
3445
// ParseError is an error type that indicates a value could not be parsed
3546
// into the expected type.
3647
type ParseError struct {
3748
Expected reflect.Value
38-
Value interface{}
49+
Value any
3950
Err error
4051
}
4152

4253
func (e *ParseError) Error() string {
43-
return fmt.Sprintf("cannot parse '%s' as '%s': %s",
44-
e.Value, e.Expected.Type(), e.Err)
54+
return fmt.Sprintf("cannot parse value as '%s': %s", e.Expected.Type(), e.Err)
4555
}
4656

57+
func (*ParseError) mapstructure() {}
58+
4 8000 759
// UnconvertibleTypeError is an error type that indicates a value could not be
4860
// converted to the expected type.
4961
type UnconvertibleTypeError struct {
5062
Expected reflect.Value
51-
Value interface{}
63+
Value any
5264
}
5365

5466
func (e *UnconvertibleTypeError) Error() string {
55-
return fmt.Sprintf("expected type '%s', got unconvertible type '%s', value: '%v'",
56-
e.Expected.Type(), reflect.TypeOf(e.Value), e.Value)
67+
return fmt.Sprintf(
68+
"expected type '%s', got unconvertible type '%s'",
69+
e.Expected.Type(),
70+
reflect.TypeOf(e.Value),
71+
)
5772
}
73+
74+
func (*UnconvertibleTypeError) mapstructure() {}

mapstructure_examples_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ func ExampleDecode_errors() {
6565
// Output:
6666
// decoding failed due to the following error(s):
6767
//
68-
// 'Name' expected type 'string', got unconvertible type 'int', value: '123'
69-
// 'Age' expected type 'int', got unconvertible type 'string', value: 'bad value'
70-
// 'Emails[0]' expected type 'string', got unconvertible type 'int', value: '1'
71-
// 'Emails[1]' expected type 'string', got unconvertible type 'int', value: '2'
72-
// 'Emails[2]' expected type 'string', got unconvertible type 'int', value: '3'
68+
// 'Name' expected type 'string', got unconvertible type 'int'
69+
// 'Age' expected type 'int', got unconvertible type 'string'
70+
// 'Emails[0]' expected type 'string', got unconvertible type 'int'
71+
// 'Emails[1]' expected type 'string', got unconvertible type 'int'
72+
// 'Emails[2]' expected type 'string', got unconvertible type 'int'
7373
}
7474

7575
func ExampleDecode_metadata() {

0 commit comments

Comments
 (0)
0