Documentation
¶
Overview ¶
Package httpx provides unified handling of HTTP requests/responses
Package httpx provides unified handling for HTTP requests/responses
The package httpx provides unified handling of HTTP requests/responses
Index ¶
- func BadRequestJson(c *gin.Context, err error)
- func ErrorJson(c *gin.Context, msg string)
- func ErrorLoggingMiddleware(cfg ErrorLoggingConfig) gin.HandlerFunc
- func HandleError(c *gin.Context, err error)
- func InternalErrorJson(c *gin.Context, msg string)
- func NoMethodHandler() gin.HandlerFunc
- func NoRouteHandler() gin.HandlerFunc
- func NotFoundJson(c *gin.Context, msg string)
- func OkJson(c *gin.Context, data interface{})
- func Parse(c *gin.Context, req interface{}) error
- func Wrap[Req any, Resp any](handler HandlerFunc[Req, Resp]) gin.HandlerFunc
- type ErrorLoggingConfig
- type HandlerFunc
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BadRequestJson ¶
BadRequestJson 400 error response
func ErrorLoggingMiddleware ¶
func ErrorLoggingMiddleware(cfg ErrorLoggingConfig) gin.HandlerFunc
Inject error logging configuration into Context After using this middleware, HandleError will decide whether to log based on the configuration.
func HandleError ¶
HandleError intelligently handles errors (returning different status codes based on error type) According to Best Practices Article 039 + Configuration Logging Article 041: Return the error code and message of LayeredError to the frontend 2. Decide based on configuration whether to log the full error chain to backend logs (default is not to log) 3. Use errors.Is to check for business error codes
func InternalErrorJson ¶
InternalErrorJson 500 error response
func NoMethodHandler ¶
func NoMethodHandler() gin.HandlerFunc
NoMethodHandler 405 Method Not Allowed Handler Used for engine.NoMethod() registration, returns a uniformly formatted JSON response
func NoRouteHandler ¶
func NoRouteHandler() gin.HandlerFunc
NoRouteHandler 404 route not found handler For registering engine.NoRoute(), returns a unified JSON response format
func Parse ¶
Parse automatically extract request parameters (query + body + path) Supports form/json/uri tags
func Wrap ¶
func Wrap[Req any, Resp any](handler HandlerFunc[Req, Resp]) gin.HandlerFunc
Wrap Packaging Handler, automatically handle parsing, validation, response Decouple business logic from HTTP details
Types ¶
type ErrorLoggingConfig ¶
type ErrorLoggingConfig struct {
// Enable error log recording (default false)
Enable bool `mapstructure:"enable" json:"enable"`
// IgnoreHTTPStatus Ignored HTTP status codes (do not log)
// For example: []int{400, 404} indicates that errors 400 and 404 are not recorded
IgnoreHTTPStatus []int `mapstructure:"ignore_http_status" json:"ignore_http_status"`
// Whether to record the full error chain (default true)
// if false, only record error_code and error_msg, do not record error_chain
FullErrorChain bool `mapstructure:"full_error_chain" json:"full_error_chain"`
// LogLevel log level: error, warn, info (default is error)
LogLevel string `mapstructure:"log_level" json:"log_level"`
}
ErrorLoggingConfig Error logging configuration
func DefaultErrorLoggingConfig ¶
func DefaultErrorLoggingConfig() ErrorLoggingConfig
DefaultErrorLoggingConfig returns the default configuration (logging disabled by default)
type HandlerFunc ¶
HandlerFunc generic handler function signature Req: Request type (supported types: form/json/uri tag) Response type