Skip to content

Commit

Permalink
feat: add Abort method (#134)
Browse files Browse the repository at this point in the history
* feat: add Abort method

* use StatusRequestTimeout

* update mod
  • Loading branch information
hwbrzzl authored Dec 30, 2024
1 parent 19f4600 commit 45db67e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
13 changes: 13 additions & 0 deletions context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,25 @@ func NewContextRequest(ctx *Context, log log.Log, validation contractsvalidate.V
return request
}

func (r *ContextRequest) Abort(code ...int) {
realCode := http.StatusBadRequest
if len(code) > 0 {
realCode = code[0]
}

if err := r.instance.SendStatus(realCode); err != nil {
panic(err)
}
}

// DEPRECATED: Use Abort instead
func (r *ContextRequest) AbortWithStatus(code int) {
if err := r.instance.SendStatus(code); err != nil {
panic(err)
}
}

// DEPRECATED: Use Response().Json().Abort() instead
func (r *ContextRequest) AbortWithStatusJson(code int, jsonObj any) {
if err := r.instance.Status(code).JSON(jsonObj); err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/gofiber/template/html/v2 v2.1.2
github.com/gookit/validate v1.5.4
github.com/goravel/framework v1.14.1-0.20241229081312-7de6cc0c5866
github.com/goravel/framework v1.14.1-0.20241230074406-15bcb208658a
github.com/savioxavier/termlink v1.4.1
github.com/spf13/cast v1.7.1
github.com/stretchr/testify v1.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ github.com/gookit/validate v1.5.4 h1:nwBo6vULnVUeNFCOde6RKFRbOCKJXVMnWR0ghedacLg
github.com/gookit/validate v1.5.4/go.mod h1:p9sRPfpvYB4vXICBpEPzv8FoAky+XhUOhWQghgmmat4=
github.com/goravel/file-rotatelogs/v2 v2.4.2 h1:g68AzbePXcm0V2CpUMc9j4qVzcDn7+7aoWSjZ51C0m4=
github.com/goravel/file-rotatelogs/v2 v2.4.2/go.mod h1:23VuSW8cBS4ax5cmbV+5AaiLpq25b8UJ96IhbAkdo8I=
github.com/goravel/framework v1.14.1-0.20241229081312-7de6cc0c5866 h1:Xz7jgacMzIN8mVzORo5xDlorANNnSDt54BhVtQ2ppM8=
github.com/goravel/framework v1.14.1-0.20241229081312-7de6cc0c5866/go.mod h1:Qo5Xlf+slrosyMxBKbNoxpEmzB6y2C5FI4BHNdVOSyI=
github.com/goravel/framework v1.14.1-0.20241230074406-15bcb208658a h1:iVjoA10Ej3VE1XEGXdsQR+75waq8BGMr9pwfOvrLGCQ=
github.com/goravel/framework v1.14.1-0.20241230074406-15bcb208658a/go.mod h1:Qo5Xlf+slrosyMxBKbNoxpEmzB6y2C5FI4BHNdVOSyI=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
2 changes: 1 addition & 1 deletion middleware_timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Timeout(timeout time.Duration) contractshttp.Middleware {
case <-done:
case <-timeoutCtx.Done():
if errors.Is(ctx.Context().Err(), context.DeadlineExceeded) {
ctx.Request().AbortWithStatusJson(http.StatusGatewayTimeout, fiber.Map{"error": "Request Timeout"})
ctx.Request().AbortWithStatusJson(http.StatusRequestTimeout, fiber.Map{"error": "Request Timeout"})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion middleware_timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestTimeoutMiddleware(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, resp)

assert.Equal(t, http.StatusGatewayTimeout, resp.StatusCode)
assert.Equal(t, http.StatusRequestTimeout, resp.StatusCode)

body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
Expand Down

0 comments on commit 45db67e

Please sign in to comment.