-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
- Loading branch information
Showing
18 changed files
with
208 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package oserror_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"internal/oserror" | ||
"os" | ||
"testing" | ||
) | ||
|
||
type ttError struct { | ||
timeout bool | ||
temporary bool | ||
} | ||
|
||
func (e ttError) Error() string { | ||
return fmt.Sprintf("ttError{timeout:%v temporary:%v}", e.timeout, e.temporary) | ||
} | ||
func (e ttError) Timeout() bool { return e.timeout } | ||
func (e ttError) Temporary() bool { return e.temporary } | ||
|
||
type isError struct { | ||
err error | ||
} | ||
|
||
func (e isError) Error() string { return fmt.Sprintf("isError(%v)", e.err) } | ||
func (e isError) Is(target error) bool { return e.err == target } | ||
|
||
func TestIsTimeout(t *testing.T) { | ||
for _, test := range []struct { | ||
want bool | ||
err error | ||
}{ | ||
{true, ttError{timeout: true}}, | ||
{true, isError{os.ErrTimeout}}, | ||
{true, os.ErrTimeout}, | ||
{true, fmt.Errorf("wrap: %w", os.ErrTimeout)}, | ||
{false, ttError{timeout: false}}, | ||
{false, errors.New("error")}, | ||
} { | ||
if got, want := oserror.IsTimeout(test.err), test.want; got != want { | ||
t.Errorf("IsTimeout(err) = %v, want %v\n%+v", got, want, test.err) | ||
} | ||
} | ||
} | ||
|
||
func TestIsTemporary(t *testing.T) { | ||
for _, test := range []struct { | ||
want bool | ||
err error | ||
}{ | ||
{true, ttError{temporary: true}}, | ||
{true, isError{os.ErrTemporary}}, | ||
{true, os.ErrTemporary}, | ||
{true, fmt.Errorf("wrap: %w", os.ErrTemporary)}, | ||
{false, ttError{temporary: false}}, | ||
{false, errors.New("error")}, | ||
} { | ||
if got, want := oserror.IsTemporary(test.err), test.want; got != want { | ||
t.Errorf("IsTemporary(err) = %v, want %v\n%+v", got, want, test.err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.