Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use errors.As instead of type assertion in IsError functions
This allows IsRedirect to detect a Redirect error even if it's wrapped via fmt.Errorf("some description: %w", err).
- Loading branch information
Showing
3 changed files
with
41 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package httperror_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/shurcooL/httperror" | ||
) | ||
|
||
func TestIsRedirect(t *testing.T) { | ||
for _, tt := range [...]struct { | ||
name string | ||
in error | ||
want bool | ||
}{ | ||
{name: "bare", in: httperror.Redirect{URL: "u"}, want: true}, | ||
{name: "wrapped in %w", in: fmt.Errorf("desc: %w", httperror.Redirect{URL: "u"}), want: true}, | ||
{name: "wrapped in %v", in: fmt.Errorf("desc: %v", httperror.Redirect{URL: "u"}), want: false}, | ||
{name: "not redirect", in: httperror.Method{Allowed: []string{"GET"}}, want: false}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, got := httperror.IsRedirect(tt.in) | ||
if got != tt.want { | ||
t.Errorf("got = %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/shurcooL/httperror | ||
|
||
go 1.17 |