Documentation
¶
Index ¶
- func BadRequest(ctx *gin.Context, condition bool, msg ...string) bool
- func ChangeResultType(f func() Resp)
- func DirectBadRequest(ctx *gin.Context, format string, args ...any)
- func DirectRespErr(ctx *gin.Context, err error)
- func DirectRespWithCode(ctx *gin.Context, bCode int, format string, args ...any)
- func Forbidden(ctx *gin.Context, condition bool, msg ...string) bool
- func Free(resp Resp)
- func GetValidMsg(err error, obj interface{}) string
- func Json(ctx *gin.Context, data any)
- func LoginExpired(ctx *gin.Context, condition bool, msg ...string) bool
- func NoLogin(ctx *gin.Context, condition bool, msg ...string) bool
- func Ok(ctx *gin.Context)
- func ParamValidation(ctx *gin.Context, obj interface{}) bool
- func ServerError(ctx *gin.Context, condition bool, msg ...string) bool
- type PaginationResult
- type Resp
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BadRequest ¶
BadRequest handles business-related errors. Returns true if the condition is true.
func ChangeResultType ¶ added in v3.0.8
func ChangeResultType(f func() Resp)
ChangeResultType changes the result type used by the result pool.
func DirectBadRequest ¶ added in v3.0.3
DirectBadRequest directly returns a business-related error.
func DirectRespErr ¶ added in v3.0.8
DirectRespErr responds directly with any error.
The function first checks if the error is of type exception.StackError. If it is, the stack trace is logged for debugging purposes, but the function continues to check for other error types.
If the error is a BusinessException, the function responds with the corresponding business error code and message.
If no specific error type is identified, a generic server error response is returned.
func DirectRespWithCode ¶ added in v3.0.3
DirectRespWithCode directly responds with a custom business code.
func Forbidden ¶ added in v3.0.6
Forbidden handles requests where the server understands the request but refuses to execute it. Returns true if the condition is true.
func GetValidMsg ¶ added in v3.2.2
GetValidMsg extracts a user-friendly error message from a validation error. It retrieves the message associated with the field in the provided object, using the custom tag or a default message if not found.
Parameters: - err: The error to extract a message from. It should be of type validator.ValidationErrors. - obj: The object used to retrieve field-specific messages from tags.
func LoginExpired ¶
LoginExpired handles cases where the user's login session has expired. Returns true if the condition is true.
func NoLogin ¶
NoLogin handles situations where the user is not logged in. Returns true if the condition is true.
func ParamValidation ¶
ParamValidation performs parameter validation. Returns false if the validation fails, and sends an appropriate error message.
Types ¶
type PaginationResult ¶
type PaginationResult struct { Total int64 `json:"total"` // Total count of items. PageSize int `json:"page_size"` // Number of items per page. PageIndex int `json:"page_index"` // Index of the current page. Data interface{} `json:"data"` // Data for the current page. }
PaginationResult represents a paginated response.
type Resp ¶
type Resp interface { // WithBasic sets the basic properties of the response. WithBasic(businessCode int, msg string, data any) Resp // WithContext sets the context, which is mandatory to avoid null pointer exceptions. WithContext(ctx *gin.Context) Resp // To sends the response to the client. The optional httpCode parameter allows specifying an HTTP status code. To(httpCode ...int) }
Resp defines the interface for responses to be sent to the client.
type Result ¶
type Result struct { Code int `json:"code"` // Business code. TraceId string `json:"trace_id,omitempty"` // Optional trace ID for tracking, can be empty. Message string `json:"msg"` // Business message. Data interface{} `json:"ret,omitempty"` // Response data, can be empty. // contains filtered or unexported fields }
Result represents a standard response structure.
func (*Result) To ¶
To sends the Result as a JSON response to the client and releases the object back to the pool.