goccy / go-yaml

YAML support for the Go language
MIT License
1.12k stars 129 forks source link

Refactor overflowError to be pretty-printable #470

Open oxzi opened 1 month ago

oxzi commented 1 month ago

When trying to yaml.Unmarshal a negative number into an uint, the resulting error indicates an overflow with type information, but without the position. For example:

cannot unmarshal -23 into Go value of type uint64 ( overflow )

From an end user's perspective reading an error for an invalid YAML, this is quite unintuitive. Especially compared to the error message when trying to unmarshal a string into an uint.

[1:4] cannot unmarshal string into Go struct field Foo.A of type uint64
    >  1 | a: 'foo'
              ^

This change has moved overflowError to internal.errors.overflowError, implementing both the PrettyPrinter and Formatter. Its implementation is uniform with those of the syntaxError and TypeError. Thus, the error from above now looks like:

[1:4] cannot unmarshal -23 into Go value of type uint64 ( overflow )
    >  1 | a: -23
              ^
codecov-commenter commented 1 month ago

:warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 78.76%. Comparing base (237df0e) to head (8bfb9ef). Report is 1 commits behind head on master.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #470 +/- ## ========================================== - Coverage 78.77% 78.76% -0.01% ========================================== Files 13 13 Lines 4004 4003 -1 ========================================== - Hits 3154 3153 -1 Misses 591 591 Partials 259 259 ```