Skip to content

Commit b82cfe5

Browse files
committed
use DecodeError as internal interface for all errors.
1 parent d0d489c commit b82cfe5

File tree

3 files changed

+123
-165
lines changed

3 files changed

+123
-165
lines changed

errors.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,53 @@ import (
55
"reflect"
66
)
77

8-
// ErrCannotDecode is a generic error type that holds information about
8+
// DecodeError is a generic error type that holds information about
99
// 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
1313
}
1414

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+
}
1720
}
1821

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 {
2337
Expected reflect.Value
2438
Value interface{}
39+
Err error
2540
}
2641

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)
3045
}
3146

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 {
3750
Expected reflect.Value
3851
Value interface{}
3952
}
4053

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)
4457
}

0 commit comments

Comments
 (0)