Help us shape the future of Go! Take the 2026 Go Developer Survey

resp

package
v0.32.41 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(c *app.RequestContext, message string)

func Error

func Error(c *app.RequestContext, code int)

Error 旧版 errors 码的错误响应(兼容老调用方)

func ErrorWithData

func ErrorWithData(c *app.RequestContext, code int, message string, data interface{})

func ErrorWithMessage

func ErrorWithMessage(c *app.RequestContext, code int, message string)

func HTTPStatusForCode added in v0.32.7

func HTTPStatusForCode(code int) int

HTTPStatusForCode 导出业务码 → HTTP 状态 的映射。

供取件闸门**契约守卫**(bootstrap/share_surface_contract_test.go)核对 「契约表登记的 HTTP 状态」与「运行时实际映射」是否一致 ⇒ 2026-10-25 审计 A9 指出的「契约表只登记业务码 ⇒ 状态码退化结构上无法被发现」由此闭合。

func InternalError

func InternalError(c *app.RequestContext, message string)

func NewErrorByCode

func NewErrorByCode(c *app.RequestContext, code int)

NewErrorByCode 新版业务码错误响应(推荐) 自动根据业务码选择 HTTP 状态:

401/403/404/429/500/503 等

func NewErrorWithData

func NewErrorWithData(c *app.RequestContext, code int, message string, data interface{})

NewErrorWithData 新版业务码 + data

func NewErrorWithMessage

func NewErrorWithMessage(c *app.RequestContext, code int, message string)

NewErrorWithMessage 新版业务码 + 自定义 message

func NewTypedError

func NewTypedError(c *app.RequestContext, err error)

NewTypedError 把实现 ErrCode() int 的 typed error(gate/moderation 等)写成 业务错误响应;未实现该接口的错误按内部错误处理。

func NotFound

func NotFound(c *app.RequestContext, message string)

func Page

func Page(c *app.RequestContext, list interface{}, total int64, page, pageSize int)

func Success

func Success(c *app.RequestContext, data interface{})

func SuccessWithMessage

func SuccessWithMessage(c *app.RequestContext, message string, data interface{})

func Unauthorized

func Unauthorized(c *app.RequestContext, message string)

func ValidTraceID

func ValidTraceID(tid string) bool

ValidTraceID 客户端自带 trace_id 白名单校验:非空、长度 ≤64、 字符集 [A-Za-z0-9._-]。原样采信任意值会把超长串回显进响应头并落入 访问日志(日志刷量/注入面),不合规一律重新生成。 供 pkg/middleware.RequestID 与本包 getOrGenTraceID 共用。

Types

type PageData

type PageData struct {
	List     interface{} `json:"list"`
	Total    int64       `json:"total"`
	Page     int         `json:"page"`
	PageSize int         `json:"page_size"`
}

type Response

type Response struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
	TraceID string      `json:"trace_id,omitempty"`
}

Response 全站统一响应 envelope

{
  "code":      业务码 (0=成功),
  "message":   业务码默认文案 / 自定义 message,
  "data":      业务数据 (omitempty),
  "trace_id":  请求链路 ID (omitempty, 用于排错)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL