Skip to content

Commit 9e37c93

Browse files
committed
removed Is method and improved StatusCode method
1 parent a16859c commit 9e37c93

2 files changed

Lines changed: 28 additions & 42 deletions

File tree

httperror.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,14 @@ func (he *HTTPError) Error() string {
7373
return fmt.Sprintf("code=%d, message=%v, err=%v", he.Code, msg, he.err.Error())
7474
}
7575

76-
// Is checks if this error is equal to the target error.
77-
func (he *HTTPError) Is(target error) bool {
78-
if he == target {
79-
return true
76+
// HTTPStatusCode returns status code from error if it implements HTTPStatusCoder interface.
77+
// If error does not implement the interface it returns 0.
78+
func HTTPStatusCode(err error) int {
79+
var sc HTTPStatusCoder
80+
if errors.As(err, &sc) {
81+
return sc.StatusCode()
8082
}
81-
switch t := target.(type) {
82-
case *HTTPError:
83-
return he.Code == t.Code
84-
case *httpError:
85-
return he.Code == t.code
86-
}
87-
return false
83+
return 0
8884
}
8985

9086
// Wrap eturns new HTTPError with given errors wrapped inside

httperror_test.go

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ package echo
55

66
import (
77
"errors"
8-
"github.com/stretchr/testify/assert"
8+
"fmt"
99
"net/http"
1010
"testing"
11+
12+
"github.com/stretchr/testify/assert"
1113
)
1214

1315
func TestHTTPError_StatusCode(t *testing.T) {
@@ -66,54 +68,42 @@ func TestNewHTTPError(t *testing.T) {
6668
assert.Equal(t, err2, err)
6769
}
6870

69-
func TestHTTPError_Is(t *testing.T) {
71+
func TestHTTPStatusCode(t *testing.T) {
7072
var testCases = []struct {
7173
name string
72-
err *HTTPError
73-
target error
74-
expect bool
74+
err error
75+
expect int
7576
}{
7677
{
77-
name: "ok, same instance",
78-
err: &HTTPError{Code: http.StatusNotFound},
79-
target: &HTTPError{Code: http.StatusNotFound},
80-
expect: true,
81-
},
82-
{
83-
name: "ok, different instance, same code",
78+
name: "ok, HTTPError",
8479
err: &HTTPError{Code: http.StatusNotFound},
85-
target: &HTTPError{Code: http.StatusNotFound, Message: "different"},
86-
expect: true,
80+
expect: http.StatusNotFound,
8781
},
8882
{
89-
name: "ok, target is sentinel error",
90-
err: &HTTPError{Code: http.StatusNotFound},
91-
target: ErrNotFound,
92-
expect: true,
83+
name: "ok, sentinel error",
84+
err: ErrNotFound,
85+
expect: http.StatusNotFound,
9386
},
9487
{
95-
name: "nok, different code",
96-
err: &HTTPError{Code: http.StatusNotFound},
97-
target: &HTTPError{Code: http.StatusInternalServerError},
98-
expect: false,
88+
name: "ok, wrapped HTTPError",
89+
err: fmt.Errorf("wrapped: %w", &HTTPError{Code: http.StatusTeapot}),
90+
expect: http.StatusTeapot,
9991
},
10092
{
101-
name: "nok, target is sentinel error with different code",
102-
err: &HTTPError{Code: http.StatusNotFound},
103-
target: ErrInternalServerError,
104-
expect: false,
93+
name: "nok, normal error",
94+
err: errors.New("error"),
95+
expect: 0,
10596
},
10697
{
107-
name: "nok, target is different error type",
108-
err: &HTTPError{Code: http.StatusNotFound},
109-
target: errors.New("some error"),
110-
expect: false,
98+
name: "nok, nil",
99+
err: nil,
100+
expect: 0,
111101
},
112102
}
113103

114104
for _, tc := range testCases {
115105
t.Run(tc.name, func(t *testing.T) {
116-
assert.Equal(t, tc.expect, errors.Is(tc.err, tc.target))
106+
assert.Equal(t, tc.expect, HTTPStatusCode(tc.err))
117107
})
118108
}
119109
}

0 commit comments

Comments
 (0)