Files
zhangdeman 52205b42c8
Verify / verify (push) Canceled after 0s
Verify / verify (pull_request) Canceled after 0s
chore(module): 更新仓库地址及模块导入路径
2026-11-27 22:58:26 -08:00

34 lines
1.0 KiB
Go

package http
import (
"context"
"errors"
"net/http"
failure "git.zhangdeman.cn/zhangdeman/ginframework/internal/failure"
"github.com/gin-gonic/gin"
)
// finishCanceled 在合作式预算结束后统一返回超时响应,保留已分类错误的日志语义。
// 入参:ctx 为当前请求,cause 为已接收的下层错误,允许 nil。
// 返回:上下文结束并应停止当前流程时为 true;客户端取消时不继续写响应。
func (s *Server) finishCanceled(ctx *gin.Context, cause error) bool {
err := ctx.Request.Context().Err()
if err == nil {
return false
}
if cause == nil {
cause = err
}
code, summary := codeRequestCancelled, "HTTP request cancelled"
if errors.Is(err, context.DeadlineExceeded) {
code, summary = codeRequestTimeout, "HTTP request budget exceeded"
}
classified := failure.WrapContext(ctx.Request.Context(), code, summary, cause)
if errors.Is(err, context.DeadlineExceeded) {
s.writeResult(ctx, nil, classified, http.StatusGatewayTimeout)
}
ctx.Abort()
return true
}