-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
51 lines (45 loc) · 1.37 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package henge
import (
"errors"
"fmt"
"reflect"
)
var (
// ErrInvalidValue is an error when the source is an invalid value.
// Refer: reflect.IsValid
ErrInvalidValue = errors.New("invalid value")
// ErrUnsupportedType is an error when the source is an unsupported type.
ErrUnsupportedType = errors.New("unsupported type")
// ErrOverflow is an error if an overflow occurs during conversion.
ErrOverflow = errors.New("overflows")
// ErrNegativeNumber is an error if converting a negative number to an unsigned type.
ErrNegativeNumber = errors.New("negative number")
// ErrNotConvertible is an error, when reflect.Value.Convert needs to use but reflect.Type.ConvertibleTo returns false.
ErrNotConvertible = errors.New("not convertible")
)
type (
// ConvertError is an error that shows where the error occurred during conversion.
ConvertError struct {
Field string
SrcType reflect.Type
DstType reflect.Type
Value interface{}
Err error
}
)
func (e *ConvertError) Unwrap() error {
return e.Err
}
func (e *ConvertError) Error() string {
srcTypeString, dstTypeString := "nil", "nil"
if e.SrcType != nil {
srcTypeString = e.SrcType.String()
}
if e.DstType != nil {
dstTypeString = e.DstType.String()
}
return fmt.Sprintf(
"Failed to convert from %s to %s: fields=%s, value=%#v, error=%s",
srcTypeString, dstTypeString, e.Field, e.Value, e.Err.Error(),
)
}