httpx

package
v0.0.0-...-fb81f76 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequestJson

func BadRequestJson(c *gin.Context, err error)

BadRequestJson 400 error response

func ErrorJson

func ErrorJson(c *gin.Context, msg string)

ErrorJson error response (400 Bad Request)

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

func HandleError(c *gin.Context, err error)

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

func InternalErrorJson(c *gin.Context, msg string)

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 NotFoundJson

func NotFoundJson(c *gin.Context, msg string)

NotFoundJson 404 error response

func OkJson

func OkJson(c *gin.Context, data interface{})

OkJson successful response

func Parse

func Parse(c *gin.Context, req interface{}) error

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

type HandlerFunc[Req any, Resp any] func(c *gin.Context, req *Req) (*Resp, error)

HandlerFunc generic handler function signature Req: Request type (supported types: form/json/uri tag) Response type

type Response

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg,omitempty"`
	Data interface{} `json:"data,omitempty"`
}

Unified response format

Directories

Path Synopsis
Package types 提供 HTTP 请求/响应的通用类型
Package types 提供 HTTP 请求/响应的通用类型

Jump to

Keyboard shortcuts

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