diff --git a/decode.go b/decode.go index 72af5e22..5e284f3d 100644 --- a/decode.go +++ b/decode.go @@ -524,19 +524,6 @@ func (d *Decoder) convertValue(v reflect.Value, typ reflect.Type, src ast.Node) return v.Convert(typ), nil } -type overflowError struct { - dstType reflect.Type - srcNum string -} - -func (e *overflowError) Error() string { - return fmt.Sprintf("cannot unmarshal %s into Go value of type %s ( overflow )", e.srcNum, e.dstType) -} - -func errOverflow(dstType reflect.Type, num string) *overflowError { - return &overflowError{dstType: dstType, srcNum: num} -} - func errTypeMismatch(dstType, srcType reflect.Type, token *token.Token) *errors.TypeError { return &errors.TypeError{DstType: dstType, SrcType: srcType, Token: token} } @@ -904,7 +891,7 @@ func (d *Decoder) decodeValue(ctx context.Context, dst reflect.Value, src ast.No default: return errTypeMismatch(valueType, reflect.TypeOf(v), src.GetToken()) } - return errOverflow(valueType, fmt.Sprint(v)) + return errors.ErrOverflow(valueType, fmt.Sprint(v), src.GetToken()) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: v := d.nodeToValue(src) switch vv := v.(type) { @@ -936,7 +923,7 @@ func (d *Decoder) decodeValue(ctx context.Context, dst reflect.Value, src ast.No default: return errTypeMismatch(valueType, reflect.TypeOf(v), src.GetToken()) } - return errOverflow(valueType, fmt.Sprint(v)) + return errors.ErrOverflow(valueType, fmt.Sprint(v), src.GetToken()) } v := reflect.ValueOf(d.nodeToValue(src)) if v.IsValid() { diff --git a/internal/errors/error.go b/internal/errors/error.go index 7f1ea9af..4d3a35d4 100644 --- a/internal/errors/error.go +++ b/internal/errors/error.go @@ -39,6 +39,11 @@ func ErrSyntax(msg string, tk *token.Token) *syntaxError { } } +// ErrOverflow creates an overflow error instance with message and a token. +func ErrOverflow(dstType reflect.Type, num string, tk *token.Token) *overflowError { + return &overflowError{dstType: dstType, srcNum: num, token: tk} +} + type baseError struct { state fmt.State verb rune @@ -164,6 +169,39 @@ func (e *wrapError) Error() string { return buf.String() } +type overflowError struct { + dstType reflect.Type + srcNum string + token *token.Token +} + +func (e *overflowError) Error() string { + return fmt.Sprintf("cannot unmarshal %s into Go value of type %s ( overflow )", e.srcNum, e.dstType) +} + +func (e *overflowError) PrettyPrint(p xerrors.Printer, colored, inclSource bool) error { + return e.FormatError(&FormatErrorPrinter{Printer: p, Colored: colored, InclSource: inclSource}) +} + +func (e *overflowError) FormatError(p xerrors.Printer) error { + var pp printer.Printer + + var colored, inclSource bool + if fep, ok := p.(*FormatErrorPrinter); ok { + colored = fep.Colored + inclSource = fep.InclSource + } + + pos := fmt.Sprintf("[%d:%d] ", e.token.Position.Line, e.token.Position.Column) + msg := pp.PrintErrorMessage(fmt.Sprintf("%s%s", pos, e.Error()), colored) + if inclSource { + msg += "\n" + pp.PrintErrorToken(e.token, colored) + } + p.Print(msg) + + return nil +} + type syntaxError struct { *baseError msg string