Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Abort method #124

Merged
merged 4 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
Loading