Skip to content

Commit d324506

Browse files
authored
Merge pull request #1559 from flimzy/wrapper
Extend HTTPError to satisfy the Go 1.13 error wrapper interface
2 parents 7533c69 + ea34bf9 commit d324506

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

echo.go

+5
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,11 @@ func (he *HTTPError) SetInternal(err error) *HTTPError {
791791
return he
792792
}
793793

794+
// Unwrap satisfies the Go 1.13 error wrapper interface.
795+
func (he *HTTPError) Unwrap() error {
796+
return he.Internal
797+
}
798+
794799
// WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
795800
func WrapHandler(h http.Handler) HandlerFunc {
796801
return func(c Context) error {

echo_go1.13_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// +build go1.13
2+
3+
package echo
4+
5+
import (
6+
"errors"
7+
"net/http"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestHTTPError_Unwrap(t *testing.T) {
14+
t.Run("non-internal", func(t *testing.T) {
15+
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
16+
"code": 12,
17+
})
18+
19+
assert.Nil(t, errors.Unwrap(err))
20+
})
21+
t.Run("internal", func(t *testing.T) {
22+
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
23+
"code": 12,
24+
})
25+
err.SetInternal(errors.New("internal error"))
26+
assert.Equal(t, "internal error", errors.Unwrap(err).Error())
27+
})
28+
}

echo_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,6 @@ func TestHTTPError(t *testing.T) {
549549
})
550550

551551
assert.Equal(t, "code=400, message=map[code:12]", err.Error())
552-
553552
})
554553
t.Run("internal", func(t *testing.T) {
555554
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{

0 commit comments

Comments
 (0)