Documentation
¶
Overview ¶
Package httpx provides HTTP utilities for gorp framework. This file exposes the standard HTTP response protocol and error types. Business handlers use these types to build consistent HTTP responses.
httpx 包提供 gorp 框架的 HTTP 工具。 本文件暴露标准 HTTP 响应协议和错误类型。 业务 handler 使用这些类型构建一致的 HTTP 响应。
Index ¶
- Constants
- func BadRequest(c transportcontract.Context, message string)
- func Error(c transportcontract.Context, err error)
- func ErrorWithData(c transportcontract.Context, err error, data any)
- func ErrorWithStatus(c transportcontract.Context, status int, err error)
- func Forbidden(c transportcontract.Context, message string)
- func InternalError(c transportcontract.Context, message string)
- func NotFound(c transportcontract.Context, message string)
- func Success(c transportcontract.Context, data any)
- func SuccessPaginated(c transportcontract.Context, items any, total int64, page, pageSize int)
- func SuccessWithMessage(c transportcontract.Context, message string, data any)
- func SuccessWithStatus(c transportcontract.Context, status int, data any)
- func Unauthorized(c transportcontract.Context, message string)
- type BizError
- type BusinessError
- type PaginatedData
- type Response
Constants ¶
const ( CodeBadRequest = 1001 // 请求参数错误 CodeForbidden = 1003 // 无权限 CodeNotFound = 1004 // 资源不存在 CodeInternalError = 1005 // 内部错误 )
业务错误码常量,与 HTTP 状态码对应但独立编码。 Business error code constants, mapped to HTTP status codes but independently encoded.
const CodeSuccess = 0
CodeSuccess is the success response code.
CodeSuccess 是成功响应码。
Variables ¶
This section is empty.
Functions ¶
func BadRequest ¶
func BadRequest(c transportcontract.Context, message string)
BadRequest writes a 400 Bad Request response with a business error code and message.
BadRequest 输出 400 Bad Request 响应,附带业务错误码和消息。
func Error ¶
func Error(c transportcontract.Context, err error)
Error writes an error response.
Error 输出错误响应。
func ErrorWithData ¶
func ErrorWithData(c transportcontract.Context, err error, data any)
ErrorWithData writes an error response and attaches extra response data.
ErrorWithData 输出错误响应,并附带额外数据。
func ErrorWithStatus ¶
func ErrorWithStatus(c transportcontract.Context, status int, err error)
ErrorWithStatus writes an error response using a caller-provided HTTP status.
ErrorWithStatus 使用调用方指定的 HTTP 状态码输出错误响应。
func Forbidden ¶
func Forbidden(c transportcontract.Context, message string)
Forbidden writes a 403 Forbidden response with a business error code and message.
Forbidden 输出 403 Forbidden 响应,附带业务错误码和消息。
func InternalError ¶
func InternalError(c transportcontract.Context, message string)
InternalError writes a 500 Internal Server Error response with a business error code and message.
InternalError 输出 500 Internal Server Error 响应,附带业务错误码和消息。
func NotFound ¶
func NotFound(c transportcontract.Context, message string)
NotFound writes a 404 Not Found response with a business error code and message.
NotFound 输出 404 Not Found 响应,附带业务错误码和消息。
func Success ¶
func Success(c transportcontract.Context, data any)
Success writes a standard success response.
Success 输出标准成功响应。
func SuccessPaginated ¶
func SuccessPaginated(c transportcontract.Context, items any, total int64, page, pageSize int)
SuccessPaginated writes a standard paginated success response.
SuccessPaginated 输出标准的分页成功响应。
func SuccessWithMessage ¶
func SuccessWithMessage(c transportcontract.Context, message string, data any)
SuccessWithMessage writes a success response with a custom message.
SuccessWithMessage 输出带自定义消息的成功响应。
func SuccessWithStatus ¶
func SuccessWithStatus(c transportcontract.Context, status int, data any)
SuccessWithStatus writes a success response with a custom HTTP status.
SuccessWithStatus 输出带自定义 HTTP 状态码的成功响应。
func Unauthorized ¶
func Unauthorized(c transportcontract.Context, message string)
Unauthorized writes a 401 Unauthorized response with a business error code and message.
Unauthorized 输出 401 Unauthorized 响应,附带业务错误码和消息。
Types ¶
type BizError ¶
type BizError struct {
// contains filtered or unexported fields
}
BizError is the default business error implementation.
BizError 是默认的业务错误实现。
func NewBizError ¶
NewBizError creates a new business error with the given code and message.
NewBizError 使用给定错误码和消息创建业务错误。
type BusinessError ¶
BusinessError describes an error carrying business code and message semantics.
BusinessError 描述带有业务错误码与消息语义的错误。