Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: invalid error unwrap for the httperror (#2753)
Signed-off-by: saltbo <saltbo@foxmail.com>
- Loading branch information
Showing
2 changed files
with
42 additions
and
4 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,36 @@ | ||
package error | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsNotFound(t *testing.T) { | ||
errs := map[error]bool{ | ||
nil: false, | ||
MakeError(ErrorNotFound, "someone not found"): true, | ||
MakeError(ErrorTooManyRequests, "too many requests"): false, | ||
errors.Wrap(MakeError(ErrorNotFound, "someone not found"), "other information"): true, | ||
errors.Wrap(MakeError(ErrorTooManyRequests, "too many requests"), "other information"): false, | ||
} | ||
|
||
for err, want := range errs { | ||
assert.Equal(t, want, IsNotFound(err)) | ||
} | ||
} | ||
|
||
func TestGetHTTPError(t *testing.T) { | ||
errs := map[int]error{ | ||
http.StatusBadRequest: MakeError(ErrorInvalidArgument, ""), | ||
http.StatusConflict: errors.Wrap(MakeError(ErrorNameExists, ""), ""), | ||
http.StatusNotFound: errors.Wrap(MakeError(ErrorNotFound, ""), ""), | ||
http.StatusTooManyRequests: errors.Wrap(MakeError(ErrorTooManyRequests, "too many requests"), "other information"), | ||
} | ||
for want, err := range errs { | ||
code, _ := GetHTTPError(err) | ||
assert.Equal(t, want, code) | ||
} | ||
} |