Skip to content

Commit

Permalink
strconv: add Unwrap to custom error types
Browse files Browse the repository at this point in the history
Updates #30322

This change adds the Unwrap method to NumError. NumError is the only custom error type of the strconv that has a nested exported error.

Change-Id: I8774886348880365a83f72a1d106276def27dffe
GitHub-Last-Rev: 712f3df
GitHub-Pull-Request: #34213
Reviewed-on: https://go-review.googlesource.com/c/go/+/194563
Run-TryBot: Bryan C. Mills <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Bryan C. Mills <[email protected]>
  • Loading branch information
psampaz authored and Bryan C. Mills committed Sep 30, 2019
1 parent ba18c7c commit 0adc89a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/strconv/atoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (e *NumError) Error() string {
return "strconv." + e.Func + ": " + "parsing " + Quote(e.Num) + ": " + e.Err.Error()
}

func (e *NumError) Unwrap() error { return e.Err }

func syntaxError(fn, str string) *NumError {
return &NumError{fn, str, ErrSyntax}
}
Expand Down
7 changes: 7 additions & 0 deletions src/strconv/atoi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,13 @@ func TestNumError(t *testing.T) {
}
}

func TestNumErrorUnwrap(t *testing.T) {
err := &NumError{Err: ErrSyntax}
if !errors.Is(err, ErrSyntax) {
t.Error("errors.Is failed, wanted success")
}
}

func BenchmarkParseInt(b *testing.B) {
b.Run("Pos", func(b *testing.B) {
benchmarkParseInt(b, 1)
Expand Down

0 comments on commit 0adc89a

Please sign in to comment.