-
Notifications
You must be signed in to change notification settings - Fork 0
/
cast_err.go
45 lines (37 loc) · 921 Bytes
/
cast_err.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
package pick
import (
"errors"
"fmt"
"strconv"
)
var (
ErrCastOverFlow = errors.New("overflow error")
ErrCastLostDecimals = errors.New("missing decimals error")
ErrCastInvalidType = errors.New("invalid type")
ErrCastInvalidSyntax = errors.New("invalid syntax")
)
type CastError struct {
inner error
originalValue any
}
func (c *CastError) Error() string {
innerStr := ""
if c.inner != nil {
innerStr = c.inner.Error()
}
return fmt.Sprintf("cast error, original value %T(%v), error: %s", c.originalValue, c.originalValue, innerStr)
}
func (c *CastError) Unwrap() error {
return c.inner
}
func newCastError(inner error, originalValue any) *CastError {
if errors.Is(inner, strconv.ErrRange) {
inner = ErrCastOverFlow
} else if errors.Is(inner, strconv.ErrSyntax) {
inner = ErrCastInvalidSyntax
}
return &CastError{
inner: inner,
originalValue: originalValue,
}
}