Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor overflowError to be pretty-printable #470

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down
38 changes: 38 additions & 0 deletions internal/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading