Documentation
¶
Overview ¶
Package httpx holds the HTTP conventions the handlers share: binding a request's query, form and JSON values (ShouldBind), the response envelope and ETags. Every response is HTTP 200 with the outcome in the body, where the code is 200 on success and otherwise that of the first xerr.CodeError in the error chain (ERROR when there is none); so that the access log still sees a failure, the error is also recorded on the request context.
Index ¶
- func GenerateETag(data []byte) string
- func HandlerErrorFromRequestContext(ctx *app.RequestContext) error
- func HttpResult(ctx *app.RequestContext, resp any, err error)
- func ParamErrorFromRequestContext(ctx *app.RequestContext) error
- func ParamErrorResult(ctx *app.RequestContext, err error)
- func ShouldBind(ctx *app.RequestContext, destination any) error
- type HTTPResult
- type ResponseErrorBean
- type ResponseSuccessBean
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateETag ¶ added in v1.20.2
GenerateETag returns the ETag of a response body, its hex SHA-256, so a client that sends it back in If-None-Match can be answered 304 Not Modified.
func HandlerErrorFromRequestContext ¶ added in v1.21.0
func HandlerErrorFromRequestContext(ctx *app.RequestContext) error
HandlerErrorFromRequestContext returns the error HttpResult reported for ctx, if any.
func HttpResult ¶ added in v1.20.2
func HttpResult(ctx *app.RequestContext, resp any, err error)
HttpResult writes the response for a handler's result (see BuildHTTPResult) and records a failure for the access log.
func ParamErrorFromRequestContext ¶ added in v1.20.2
func ParamErrorFromRequestContext(ctx *app.RequestContext) error
ParamErrorFromRequestContext returns the parameter error recorded for ctx.
func ParamErrorResult ¶ added in v1.20.2
func ParamErrorResult(ctx *app.RequestContext, err error)
ParamErrorResult writes the response for a request that failed binding or validation (see BuildParamErrorResult) and records the error for the access log.
func ShouldBind ¶
func ShouldBind(ctx *app.RequestContext, destination any) error
ShouldBind preserves endpoint request-binding behavior for native Hertz contexts.
Types ¶
type HTTPResult ¶ added in v1.20.2
HTTPResult is a response before it is written: its status and envelope.
func BuildHTTPResult ¶ added in v1.20.2
func BuildHTTPResult(resp any, err error) HTTPResult
BuildHTTPResult builds the response for a handler's result: resp in the success envelope, or the code and message of the first xerr.CodeError in err's chain. Any other error is reported as ERROR with a generic message, so its text never reaches the client.
func BuildParamErrorResult ¶ added in v1.20.2
func BuildParamErrorResult(err error) HTTPResult
BuildParamErrorResult builds the response for a request that failed binding or validation: InvalidParams with err's message, which tells the client what to fix.
type ResponseErrorBean ¶ added in v1.20.2
type ResponseErrorBean struct {
Code uint32 `json:"code"`
Msg string `json:"msg"`
} // @name result.ResponseErrorBean
ResponseErrorBean is the body of a failed response.
func Error ¶ added in v1.20.2
func Error(errCode uint32, errMsg string) *ResponseErrorBean
Error builds the failure envelope for errCode and errMsg.
type ResponseSuccessBean ¶ added in v1.20.2
type ResponseSuccessBean struct {
Code uint32 `json:"code"`
Msg string `json:"msg"`
Data any `json:"data,omitempty"`
} // @name result.ResponseSuccessBean
ResponseSuccessBean is the body of a successful response.
func Success ¶ added in v1.20.2
func Success(data any) *ResponseSuccessBean
Success wraps data in the success envelope: code 200, message "success".