Skip to content

Commit

Permalink
Merge pull request #73 from denopink/denopink/bug-OutdatedErrorAssert…
Browse files Browse the repository at this point in the history
…-testWRPHandlerDecodeError

bug: Patch outdated error assert for `testWRPHandlerDecodeError`
  • Loading branch information
denopink authored May 2, 2022
2 parents 294798a + 3cb6fbf commit 7f41972
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions wrphttp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package wrphttp

import "errors"

type httpError struct {
err error
code int
Expand All @@ -28,3 +30,8 @@ func (e httpError) Error() string {
func (e httpError) StatusCode() int {
return e.code
}

// Is reports whether any error in e.err's chain matches target.
func (e httpError) Is(target error) bool {
return errors.Is(e.err, target)
}
17 changes: 14 additions & 3 deletions wrphttp/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package wrphttp
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
Expand Down Expand Up @@ -201,8 +202,9 @@ func testWRPHandlerDecodeError(t *testing.T) {
assert = assert.New(t)
require = require.New(t)

expectedCtx = context.WithValue(context.Background(), foo, "bar")
expectedErr = errors.New("expected")
expectedCtx = context.WithValue(context.Background(), foo, "bar")
expectedErr = errors.New("expected")
expectedHTTPStatusCode = http.StatusBadRequest

decoder = func(actualCtx context.Context, httpRequest *http.Request) (*Entity, error) {
assert.Equal(expectedCtx, actualCtx)
Expand All @@ -213,7 +215,16 @@ func testWRPHandlerDecodeError(t *testing.T) {
errorEncoder = func(actualCtx context.Context, actualErr error, _ http.ResponseWriter) {
errorEncoderCalled = true
assert.Equal(expectedCtx, actualCtx)
assert.Equal(expectedErr, actualErr)
assert.ErrorIs(actualErr, expectedErr,
fmt.Errorf("error [%v] doesn't contain error [%v] in its err chain",
actualErr, expectedErr))

var actualErrorHTTP httpError
if assert.ErrorAs(actualErr, &actualErrorHTTP,
fmt.Errorf("error [%v] doesn't contain error [%v] in its err chain",
actualErr, actualErrorHTTP)) {
assert.Equal(expectedHTTPStatusCode, actualErrorHTTP.StatusCode())
}
}

wrpHandler = new(MockHandler)
Expand Down

0 comments on commit 7f41972

Please sign in to comment.