Skip to content

Commit

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

* optimize log

* use StatusRequestTimeout

* update mod
  • Loading branch information
hwbrzzl authored Dec 30, 2024
1 parent 6282f6e commit 9d244a7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
13 changes: 12 additions & 1 deletion context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewContextRequest(ctx *Context, log log.Log, validation contractsvalidate.V
request := contextRequestPool.Get().(*ContextRequest)
httpBody, err := getHttpBody(ctx)
if err != nil {
LogFacade.Error(fmt.Sprintf("%+v", errors.Unwrap(err)))
log.Error(fmt.Sprintf("%+v", err))
}
request.ctx = ctx
request.instance = ctx.instance
Expand All @@ -54,10 +54,21 @@ 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]
}

r.instance.AbortWithStatus(realCode)
}

// DEPRECATED: Use Abort instead.
func (r *ContextRequest) AbortWithStatus(code int) {
r.instance.AbortWithStatus(code)
}

// DEPRECATED: Use Response().Json().Abort() instead.
func (r *ContextRequest) AbortWithStatusJson(code int, jsonObj any) {
r.instance.AbortWithStatusJSON(code, jsonObj)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.23.4
require (
github.com/gin-gonic/gin v1.10.0
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/rs/cors v1.11.1
github.com/savioxavier/termlink v1.4.1
github.com/spf13/cast v1.7.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,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 @@ -34,7 +34,7 @@ func Timeout(timeout time.Duration) contractshttp.Middleware {
case <-done:
case <-timeoutCtx.Done():
if errors.Is(timeoutCtx.Err(), context.DeadlineExceeded) {
ctx.Request().AbortWithStatus(http.StatusGatewayTimeout)
ctx.Request().AbortWithStatus(http.StatusRequestTimeout)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion middleware_timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestTimeoutMiddleware(t *testing.T) {
require.NoError(t, err)

route.ServeHTTP(w, req)
assert.Equal(t, http.StatusGatewayTimeout, w.Code)
assert.Equal(t, http.StatusRequestTimeout, w.Code)
})

t.Run("normal request", func(t *testing.T) {
Expand Down

0 comments on commit 9d244a7

Please sign in to comment.