1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:14:46 -07:00

strconv: add Unwrap to custom error types

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: 712f3df884
GitHub-Pull-Request: golang/go#34213
Reviewed-on: https://go-review.googlesource.com/c/go/+/194563
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
Pantelis Sampaziotis 2019-09-10 18:43:15 +00:00 committed by Bryan C. Mills
parent ba18c7c42d
commit 0adc89aa96
2 changed files with 9 additions and 0 deletions

View File

@ -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}
}

View File

@ -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)