@@ -5,40 +5,53 @@ import (
5
5
"reflect"
6
6
)
7
7
8
- // ErrCannotDecode is a generic error type that holds information about
8
+ // DecodeError is a generic error type that holds information about
9
9
// a decoding error together with the name of the field that caused the error.
10
- type ErrCannotDecode struct {
11
- Name string
12
- Err error
10
+ type DecodeError struct {
11
+ name string
12
+ err error
13
13
}
14
14
15
- func (e * ErrCannotDecode ) Error () string {
16
- return fmt .Sprintf ("'%s': %s" , e .Name , e .Err )
15
+ func newDecodeError (name string , err error ) * DecodeError {
16
+ return & DecodeError {
17
+ name : name ,
18
+ err : err ,
19
+ }
17
20
}
18
21
19
- // ErrCannotParse extends ErrCannotDecode to include additional information
20
- // about the expected type and the actual value that could not be parsed.
21
- type ErrCannotParse struct {
22
- ErrCannotDecode
22
+ func (e * DecodeError ) Name () string {
23
+ return e .name
24
+ }
25
+
26
+ func (e * DecodeError ) Unwrap () error {
27
+ return e .err
28
+ }
29
+
30
+ func (e * DecodeError ) Error () string {
31
+ return fmt .Sprintf ("'%s' %s" , e .name , e .err )
32
+ }
33
+
34
+ // ParseError is an error type that indicates a value could not be parsed
35
+ // into the expected type.
36
+ type ParseError struct {
23
37
Expected reflect.Value
24
38
Value interface {}
39
+ Err error
25
40
}
26
41
27
- func (e * ErrCannotParse ) Error () string {
28
- return fmt .Sprintf ("'%s' cannot parse '%s' as '%s': %s" ,
29
- e .Name , e . Value , e .Expected .Type (), e .Err )
42
+ func (e * ParseError ) Error () string {
43
+ return fmt .Sprintf ("cannot parse '%s' as '%s': %s" ,
44
+ e .Value , e .Expected .Type (), e .Err )
30
45
}
31
46
32
- // ErrUnconvertibleType is an error type that indicates a value could not be
33
- // converted to the expected type. It includes the name of the field, the
34
- // expected type, and the actual value that was attempted to be converted.
35
- type ErrUnconvertibleType struct {
36
- Name string
47
+ // UnconvertibleTypeError is an error type that indicates a value could not be
48
+ // converted to the expected type.
49
+ type UnconvertibleTypeError struct {
37
50
Expected reflect.Value
38
51
Value interface {}
39
52
}
40
53
41
- func (e * ErrUnconvertibleType ) Error () string {
42
- return fmt .Sprintf ("'%s' expected type '%s', got unconvertible type '%s', value: '%v'" ,
43
- e .Name , e . Expected .Type (), reflect .TypeOf (e .Value ), e .Value )
54
+ 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 )
44
57
}
0 commit comments