mirror of
https://github.com/labstack/echo.git
synced 2025-07-03 00:56:59 +02:00
Add new method HTTPError.WithInternal
This commit is contained in:
13
echo.go
13
echo.go
@ -884,6 +884,15 @@ func (he *HTTPError) SetInternal(err error) *HTTPError {
|
|||||||
return he
|
return he
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithInternal returns clone of HTTPError with err set to HTTPError.Internal field
|
||||||
|
func (he *HTTPError) WithInternal(err error) *HTTPError {
|
||||||
|
return &HTTPError{
|
||||||
|
Code: he.Code,
|
||||||
|
Message: he.Message,
|
||||||
|
Internal: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Unwrap satisfies the Go 1.13 error wrapper interface.
|
// Unwrap satisfies the Go 1.13 error wrapper interface.
|
||||||
func (he *HTTPError) Unwrap() error {
|
func (he *HTTPError) Unwrap() error {
|
||||||
return he.Internal
|
return he.Internal
|
||||||
@ -913,8 +922,8 @@ func WrapMiddleware(m func(http.Handler) http.Handler) MiddlewareFunc {
|
|||||||
|
|
||||||
// GetPath returns RawPath, if it's empty returns Path from URL
|
// GetPath returns RawPath, if it's empty returns Path from URL
|
||||||
// Difference between RawPath and Path is:
|
// Difference between RawPath and Path is:
|
||||||
// * Path is where request path is stored. Value is stored in decoded form: /%47%6f%2f becomes /Go/.
|
// - Path is where request path is stored. Value is stored in decoded form: /%47%6f%2f becomes /Go/.
|
||||||
// * RawPath is an optional field which only gets set if the default encoding is different from Path.
|
// - RawPath is an optional field which only gets set if the default encoding is different from Path.
|
||||||
func GetPath(r *http.Request) string {
|
func GetPath(r *http.Request) string {
|
||||||
path := r.URL.RawPath
|
path := r.URL.RawPath
|
||||||
if path == "" {
|
if path == "" {
|
||||||
|
22
echo_test.go
22
echo_test.go
@ -1206,13 +1206,22 @@ func TestHTTPError(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, "code=400, message=map[code:12]", err.Error())
|
assert.Equal(t, "code=400, message=map[code:12]", err.Error())
|
||||||
})
|
})
|
||||||
t.Run("internal", func(t *testing.T) {
|
|
||||||
|
t.Run("internal and SetInternal", func(t *testing.T) {
|
||||||
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
||||||
"code": 12,
|
"code": 12,
|
||||||
})
|
})
|
||||||
err.SetInternal(errors.New("internal error"))
|
err.SetInternal(errors.New("internal error"))
|
||||||
assert.Equal(t, "code=400, message=map[code:12], internal=internal error", err.Error())
|
assert.Equal(t, "code=400, message=map[code:12], internal=internal error", err.Error())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("internal and WithInternal", func(t *testing.T) {
|
||||||
|
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
||||||
|
"code": 12,
|
||||||
|
})
|
||||||
|
err = err.WithInternal(errors.New("internal error"))
|
||||||
|
assert.Equal(t, "code=400, message=map[code:12], internal=internal error", err.Error())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHTTPError_Unwrap(t *testing.T) {
|
func TestHTTPError_Unwrap(t *testing.T) {
|
||||||
@ -1223,13 +1232,22 @@ func TestHTTPError_Unwrap(t *testing.T) {
|
|||||||
|
|
||||||
assert.Nil(t, errors.Unwrap(err))
|
assert.Nil(t, errors.Unwrap(err))
|
||||||
})
|
})
|
||||||
t.Run("internal", func(t *testing.T) {
|
|
||||||
|
t.Run("unwrap internal and SetInternal", func(t *testing.T) {
|
||||||
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
||||||
"code": 12,
|
"code": 12,
|
||||||
})
|
})
|
||||||
err.SetInternal(errors.New("internal error"))
|
err.SetInternal(errors.New("internal error"))
|
||||||
assert.Equal(t, "internal error", errors.Unwrap(err).Error())
|
assert.Equal(t, "internal error", errors.Unwrap(err).Error())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("unwrap internal and WithInternal", func(t *testing.T) {
|
||||||
|
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
||||||
|
"code": 12,
|
||||||
|
})
|
||||||
|
err = err.WithInternal(errors.New("internal error"))
|
||||||
|
assert.Equal(t, "internal error", errors.Unwrap(err).Error())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDefaultHTTPErrorHandler(t *testing.T) {
|
func TestDefaultHTTPErrorHandler(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user