1
0
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:
toimtoimtoim
2022-11-19 22:05:53 +02:00
committed by Martti T
parent fd2b102d3e
commit be23ab67cc
2 changed files with 52 additions and 25 deletions

55
echo.go
View File

@ -3,34 +3,34 @@ Package echo implements high performance, minimalist Go web framework.
Example: Example:
package main package main
import ( import (
"net/http" "net/http"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
) )
// Handler // Handler
func hello(c echo.Context) error { func hello(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!") return c.String(http.StatusOK, "Hello, World!")
} }
func main() { func main() {
// Echo instance // Echo instance
e := echo.New() e := echo.New()
// Middleware // Middleware
e.Use(middleware.Logger()) e.Use(middleware.Logger())
e.Use(middleware.Recover()) e.Use(middleware.Recover())
// Routes // Routes
e.GET("/", hello) e.GET("/", hello)
// Start server // Start server
e.Logger.Fatal(e.Start(":1323")) e.Logger.Fatal(e.Start(":1323"))
} }
Learn more at https://echo.labstack.com Learn more at https://echo.labstack.com
*/ */
@ -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 == "" {

View File

@ -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) {