Skip to content

Commit 36c3eaf

Browse files
committed
Add new method HTTPError.WithInternal
1 parent fd2b102 commit 36c3eaf

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

echo.go

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@ Package echo implements high performance, minimalist Go web framework.
33
44
Example:
55
6-
package main
6+
package main
77
8-
import (
9-
"net/http"
8+
import (
9+
"net/http"
1010
11-
"github.com/labstack/echo/v4"
12-
"github.com/labstack/echo/v4/middleware"
13-
)
11+
"github.com/labstack/echo/v4"
12+
"github.com/labstack/echo/v4/middleware"
13+
)
1414
15-
// Handler
16-
func hello(c echo.Context) error {
17-
return c.String(http.StatusOK, "Hello, World!")
18-
}
15+
// Handler
16+
func hello(c echo.Context) error {
17+
return c.String(http.StatusOK, "Hello, World!")
18+
}
1919
20-
func main() {
21-
// Echo instance
22-
e := echo.New()
20+
func main() {
21+
// Echo instance
22+
e := echo.New()
2323
24-
// Middleware
25-
e.Use(middleware.Logger())
26-
e.Use(middleware.Recover())
24+
// Middleware
25+
e.Use(middleware.Logger())
26+
e.Use(middleware.Recover())
2727
28-
// Routes
29-
e.GET("/", hello)
28+
// Routes
29+
e.GET("/", hello)
3030
31-
// Start server
32-
e.Logger.Fatal(e.Start(":1323"))
33-
}
31+
// Start server
32+
e.Logger.Fatal(e.Start(":1323"))
33+
}
3434
3535
Learn more at https://echo.labstack.com
3636
*/
@@ -884,6 +884,15 @@ func (he *HTTPError) SetInternal(err error) *HTTPError {
884884
return he
885885
}
886886

887+
// WithInternal returns clone of HTTPError with err set to HTTPError.Internal field
888+
func (he *HTTPError) WithInternal(err error) *HTTPError {
889+
return &HTTPError{
890+
Code: he.Code,
891+
Message: he.Message,
892+
Internal: err,
893+
}
894+
}
895+
887896
// Unwrap satisfies the Go 1.13 error wrapper interface.
888897
func (he *HTTPError) Unwrap() error {
889898
return he.Internal
@@ -913,8 +922,8 @@ func WrapMiddleware(m func(http.Handler) http.Handler) MiddlewareFunc {
913922

914923
// GetPath returns RawPath, if it's empty returns Path from URL
915924
// Difference between RawPath and Path is:
916-
// * Path is where request path is stored. Value is stored in decoded form: /%47%6f%2f becomes /Go/.
917-
// * RawPath is an optional field which only gets set if the default encoding is different from Path.
925+
// - Path is where request path is stored. Value is stored in decoded form: /%47%6f%2f becomes /Go/.
926+
// - RawPath is an optional field which only gets set if the default encoding is different from Path.
918927
func GetPath(r *http.Request) string {
919928
path := r.URL.RawPath
920929
if path == "" {

echo_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,13 +1206,22 @@ func TestHTTPError(t *testing.T) {
12061206

12071207
assert.Equal(t, "code=400, message=map[code:12]", err.Error())
12081208
})
1209-
t.Run("internal", func(t *testing.T) {
1209+
1210+
t.Run("internal and SetInternal", func(t *testing.T) {
12101211
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
12111212
"code": 12,
12121213
})
12131214
err.SetInternal(errors.New("internal error"))
12141215
assert.Equal(t, "code=400, message=map[code:12], internal=internal error", err.Error())
12151216
})
1217+
1218+
t.Run("internal and WithInternal", func(t *testing.T) {
1219+
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
1220+
"code": 12,
1221+
})
1222+
err = err.WithInternal(errors.New("internal error"))
1223+
assert.Equal(t, "code=400, message=map[code:12], internal=internal error", err.Error())
1224+
})
12161225
}
12171226

12181227
func TestHTTPError_Unwrap(t *testing.T) {
@@ -1223,13 +1232,22 @@ func TestHTTPError_Unwrap(t *testing.T) {
12231232

12241233
assert.Nil(t, errors.Unwrap(err))
12251234
})
1226-
t.Run("internal", func(t *testing.T) {
1235+
1236+
t.Run("unwrap internal and SetInternal", func(t *testing.T) {
12271237
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
12281238
"code": 12,
12291239
})
12301240
err.SetInternal(errors.New("internal error"))
12311241
assert.Equal(t, "internal error", errors.Unwrap(err).Error())
12321242
})
1243+
1244+
t.Run("unwrap internal and WithInternal", func(t *testing.T) {
1245+
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
1246+
"code": 12,
1247+
})
1248+
err = err.WithInternal(errors.New("internal error"))
1249+
assert.Equal(t, "internal error", errors.Unwrap(err).Error())
1250+
})
12331251
}
12341252

12351253
func TestDefaultHTTPErrorHandler(t *testing.T) {

0 commit comments

Comments
 (0)