aboutsummaryrefslogtreecommitdiff
path: root/src/context
diff options
context:
space:
mode:
authorDamien Neil <dneil@google.com>2019-03-27 08:40:12 -0700
committerDamien Neil <dneil@google.com>2019-05-04 16:14:12 +0000
commit170b8b4b12be50eeccbcdadb8523fb4fc670ca72 (patch)
treef379b48b3e9fd304aef9140983b1a2d2ecbe60fa /src/context
parent59ea685ce96a696281e56badd80d2e5dd618b0b0 (diff)
downloadgo-170b8b4b12be50eeccbcdadb8523fb4fc670ca72.tar.gz
go-170b8b4b12be50eeccbcdadb8523fb4fc670ca72.zip
all: add Unwrap and Is methods to various error types
Add Unwrap methods to types which wrap an underlying error: "encodinc/csv".ParseError "encoding/json".MarshalerError "net/http".transportReadFromServerError "net".OpError "net".DNSConfigError "net/url".Error "os/exec".Error "signal/internal/pty".PtyError "text/template".ExecError Add os.ErrTemporary. A case could be made for putting this error value in package net, since no exported error types in package os include a Temporary method. However, syscall errors returned from the os package do include this method. Add Is methods to error types with a Timeout or Temporary method, making errors.Is(err, os.Err{Timeout,Temporary}) equivalent to testing the corresponding method: "context".DeadlineExceeded "internal/poll".TimeoutError "net".adrinfoErrno "net".OpError "net".DNSError "net/http".httpError "net/http".tlsHandshakeTimeoutError "net/pipe".timeoutError "net/url".Error Updates #30322 Updates #29934 Change-Id: I409fb20c072ea39116ebfb8c7534d493483870dc Reviewed-on: https://go-review.googlesource.com/c/go/+/170037 Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Diffstat (limited to 'src/context')
-rw-r--r--src/context/context.go4
-rw-r--r--src/context/context_test.go5
2 files changed, 9 insertions, 0 deletions
diff --git a/src/context/context.go b/src/context/context.go
index 93bf5b627d..0f36881b1e 100644
--- a/src/context/context.go
+++ b/src/context/context.go
@@ -49,6 +49,7 @@ package context
import (
"errors"
+ "internal/oserror"
"internal/reflectlite"
"sync"
"time"
@@ -162,6 +163,9 @@ type deadlineExceededError struct{}
func (deadlineExceededError) Error() string { return "context deadline exceeded" }
func (deadlineExceededError) Timeout() bool { return true }
func (deadlineExceededError) Temporary() bool { return true }
+func (deadlineExceededError) Is(target error) bool {
+ return target == oserror.ErrTimeout || target == oserror.ErrTemporary
+}
// An emptyCtx is never canceled, has no values, and has no deadline. It is not
// struct{}, since vars of this type must have distinct addresses.
diff --git a/src/context/context_test.go b/src/context/context_test.go
index 0e69e2f6fd..9991c5b09c 100644
--- a/src/context/context_test.go
+++ b/src/context/context_test.go
@@ -5,8 +5,10 @@
package context
import (
+ "errors"
"fmt"
"math/rand"
+ "os"
"runtime"
"strings"
"sync"
@@ -647,4 +649,7 @@ func XTestDeadlineExceededSupportsTimeout(t testingT) {
if !i.Timeout() {
t.Fatal("wrong value for timeout")
}
+ if !errors.Is(DeadlineExceeded, os.ErrTimeout) {
+ t.Fatal("errors.Is(DeadlineExceeded, os.ErrTimeout) = false, want true")
+ }
}