]> Cypherpunks repositories - gostls13.git/commitdiff
os: add PathError.Unwrap
authorJonathan Amsterdam <jba@google.com>
Mon, 18 Mar 2019 12:06:49 +0000 (08:06 -0400)
committerJonathan Amsterdam <jba@google.com>
Wed, 20 Mar 2019 10:26:55 +0000 (10:26 +0000)
Add an Unwrap method to PathError so it works with the errors.Is/As
functions.

Change-Id: Ia6171c0418584f3cd53ee99d97c687941a9e3109
Reviewed-on: https://go-review.googlesource.com/c/go/+/168097
Reviewed-by: Damien Neil <dneil@google.com>
src/os/error.go
src/os/error_test.go

index b4242a4829c32180e7957c267c74593ba07626ea..16e5cb5786b59315f4b07aa16a9f1b81d2075350 100644 (file)
@@ -32,6 +32,8 @@ type PathError struct {
 
 func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }
 
+func (e *PathError) Unwrap() error { return e.Err }
+
 // Timeout reports whether this error represents a timeout.
 func (e *PathError) Timeout() bool {
        t, ok := e.Err.(timeout)
index 3499ceec959e5d6ac99a9ed5dd9e07b813f7df78..0e3570996ec9dae311cdb18fa2aa4a998b845c8b 100644 (file)
@@ -5,6 +5,7 @@
 package os_test
 
 import (
+       "errors"
        "fmt"
        "io/ioutil"
        "os"
@@ -155,3 +156,10 @@ func TestErrPathNUL(t *testing.T) {
                t.Fatal("Open should have failed")
        }
 }
+
+func TestPathErrorUnwrap(t *testing.T) {
+       pe := &os.PathError{Err: os.ErrInvalid}
+       if !errors.Is(pe, os.ErrInvalid) {
+               t.Error("errors.Is failed, wanted success")
+       }
+}