]> Cypherpunks repositories - gostls13.git/commitdiff
strconv: add Unwrap to custom error types
authorPantelis Sampaziotis <psampaz@gmail.com>
Tue, 10 Sep 2019 18:43:15 +0000 (18:43 +0000)
committerBryan C. Mills <bcmills@google.com>
Mon, 30 Sep 2019 21:07:51 +0000 (21:07 +0000)
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: 712f3df8842f48f988cebfc527476781a7cf7140
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>
src/strconv/atoi.go
src/strconv/atoi_test.go

index 131b088e31b5f0522470e0d9b33488f3fd573e98..a4a8a37fb4252589370dd9919d7e26ec7f4e35a3 100644 (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}
 }
index b167c96833c0738618d1cf33441db8249ea19e06..178fb01ea7edac7f3178de5c0a1c1b7523bded43 100644 (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)