Commit b41eef24 authored by Jonathan Amsterdam's avatar Jonathan Amsterdam

os: add PathError.Unwrap

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/+/168097Reviewed-by: default avatarDamien Neil <dneil@google.com>
parent 9e0e9ac5
...@@ -32,6 +32,8 @@ type PathError struct { ...@@ -32,6 +32,8 @@ type PathError struct {
func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() } 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. // Timeout reports whether this error represents a timeout.
func (e *PathError) Timeout() bool { func (e *PathError) Timeout() bool {
t, ok := e.Err.(timeout) t, ok := e.Err.(timeout)
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package os_test package os_test
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
...@@ -155,3 +156,10 @@ func TestErrPathNUL(t *testing.T) { ...@@ -155,3 +156,10 @@ func TestErrPathNUL(t *testing.T) {
t.Fatal("Open should have failed") 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")
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment