-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[xerrors] implement
IsTimeout
helper function (#7)
- Loading branch information
1 parent
7a4dfc3
commit 09374b3
Showing
4 changed files
with
61 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
go 1.22.2 | ||
go 1.23.4 | ||
|
||
use ( | ||
./xerrors | ||
./xmaps | ||
./xmath | ||
./xnet/xhttp | ||
|
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,21 @@ | ||
package xerrors | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// IsTimeout reports whether the provided error indicates a timeout condition. | ||
func IsTimeout(err error) bool { | ||
if err == nil { | ||
return false | ||
} | ||
|
||
e, ok := err.(interface{ Timeout() bool }) | ||
if ok && e.Timeout() { | ||
return true | ||
} | ||
|
||
inner := errors.Unwrap(err) | ||
|
||
return inner != nil && IsTimeout(inner) | ||
} |
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,35 @@ | ||
package xerrors_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/kucherenkovova/gopypaste/xerrors" | ||
) | ||
|
||
type customTimeoutError struct{} | ||
|
||
func (e customTimeoutError) Error() string { return "timeout" } | ||
|
||
func (e customTimeoutError) Timeout() bool { return true } | ||
|
||
func TestIsTimeout(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
err error | ||
want bool | ||
}{ | ||
{name: "nil", err: nil, want: false}, | ||
{name: "context timeout", err: context.DeadlineExceeded, want: true}, | ||
{name: "wrapped context timeout", err: fmt.Errorf("bad stuff: %w", context.DeadlineExceeded), want: true}, | ||
{name: "custom timeout", err: customTimeoutError{}, want: true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := xerrors.IsTimeout(tt.err); got != tt.want { | ||
t.Errorf("IsTimeout() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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,3 @@ | ||
module github.com/kucherenkovova/gopypaste/xerrors | ||
|
||
go 1.23.4 |