🔀 Merge branch 'bugfix/abort-after-err' into develop
This commit is contained in:
commit
30765808e2
3 changed files with 52 additions and 2 deletions
|
|
@ -25,6 +25,8 @@ func HandlerWithErrorWrapper(h HandlerWithError) gin.HandlerFunc {
|
|||
return func(c *gin.Context) {
|
||||
err := h(c)
|
||||
if err != nil {
|
||||
// We shouldn't process more handlers if there was an error
|
||||
c.Abort()
|
||||
c.Error(err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
47
int_test.go
Normal file
47
int_test.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
errors "codeberg.org/danjones000/responsable-errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestErrorMiddleware(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
_, r := gin.CreateTestContext(w)
|
||||
r.Use(ErrorMiddleware())
|
||||
r.Use(HandlerWithErrorWrapper(func(c *gin.Context) error {
|
||||
return errors.Errorf(400, "Oops")
|
||||
}))
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"Hello": "World"})
|
||||
})
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 400, w.Code)
|
||||
assert.Equal(t, `{"error":"Oops"}`, w.Body.String())
|
||||
}
|
||||
|
||||
func TestErrorNoResponseIfAlreadyWritten(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
_, r := gin.CreateTestContext(w)
|
||||
r.Use(ErrorMiddleware())
|
||||
|
||||
r.GET("/", HandlerWithErrorWrapper(func(c *gin.Context) error {
|
||||
c.JSON(200, gin.H{"Hello": "World"})
|
||||
return errors.Errorf(400, "Oops")
|
||||
}))
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 200, w.Code)
|
||||
assert.Equal(t, `{"Hello":"World"}`, w.Body.String())
|
||||
|
||||
}
|
||||
|
|
@ -55,7 +55,8 @@ func ErrorMiddleware(opts ...Option) gin.HandlerFunc {
|
|||
|
||||
c.Set("rendered_error", re)
|
||||
|
||||
// @todo check a response hasn't already been sent
|
||||
if !c.Writer.Written() {
|
||||
c.JSON(re.Status(), re)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue