diff --git a/decode.go b/decode.go index ce2c751e..b159e49b 100644 --- a/decode.go +++ b/decode.go @@ -515,19 +515,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} } @@ -895,7 +882,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) { @@ -927,7 +914,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 1a137c80..c654bd51 100644 --- a/internal/errors/error.go +++ b/internal/errors/error.go @@ -40,6 +40,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 @@ -166,6 +171,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