1
0
mirror of https://github.com/labstack/echo.git synced 2025-02-15 13:53:06 +02:00

Merge pull request #1559 from flimzy/wrapper

Extend HTTPError to satisfy the Go 1.13 error wrapper interface
This commit is contained in:
Roland Lammel 2020-07-23 21:51:54 +02:00 committed by GitHub
commit d3245067e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View File

@ -791,6 +791,11 @@ func (he *HTTPError) SetInternal(err error) *HTTPError {
return he
}
// Unwrap satisfies the Go 1.13 error wrapper interface.
func (he *HTTPError) Unwrap() error {
return he.Internal
}
// WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
func WrapHandler(h http.Handler) HandlerFunc {
return func(c Context) error {

28
echo_go1.13_test.go Normal file
View File

@ -0,0 +1,28 @@
// +build go1.13
package echo
import (
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHTTPError_Unwrap(t *testing.T) {
t.Run("non-internal", func(t *testing.T) {
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
"code": 12,
})
assert.Nil(t, errors.Unwrap(err))
})
t.Run("internal", func(t *testing.T) {
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
"code": 12,
})
err.SetInternal(errors.New("internal error"))
assert.Equal(t, "internal error", errors.Unwrap(err).Error())
})
}

View File

@ -549,7 +549,6 @@ func TestHTTPError(t *testing.T) {
})
assert.Equal(t, "code=400, message=map[code:12]", err.Error())
})
t.Run("internal", func(t *testing.T) {
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{