resp

package
v3.3.5 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(ctx *gin.Context, condition bool, msg ...string) bool

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

func DirectBadRequest(ctx *gin.Context, format string, args ...any)

DirectBadRequest directly returns a business-related error.

func DirectRespErr added in v3.0.8

func DirectRespErr(ctx *gin.Context, err error)

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

func DirectRespWithCode(ctx *gin.Context, bCode int, format string, args ...any)

DirectRespWithCode directly responds with a custom business code.

func Forbidden added in v3.0.6

func Forbidden(ctx *gin.Context, condition bool, msg ...string) bool

Forbidden handles requests where the server understands the request but refuses to execute it. Returns true if the condition is true.

func Free added in v3.3.0

func Free(resp Resp)

Free releases the result back to the pool.

func GetValidMsg added in v3.2.2

func GetValidMsg(err error, obj interface{}) string

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 Json

func Json(ctx *gin.Context, data any)

Json sends a standard success response with data.

func LoginExpired

func LoginExpired(ctx *gin.Context, condition bool, msg ...string) bool

LoginExpired handles cases where the user's login session has expired. Returns true if the condition is true.

func NoLogin

func NoLogin(ctx *gin.Context, condition bool, msg ...string) bool

NoLogin handles situations where the user is not logged in. Returns true if the condition is true.

func Ok

func Ok(ctx *gin.Context)

Ok sends a standard success response with no data.

func ParamValidation

func ParamValidation(ctx *gin.Context, obj interface{}) bool

ParamValidation performs parameter validation. Returns false if the validation fails, and sends an appropriate error message.

func ServerError added in v3.3.0

func ServerError(ctx *gin.Context, condition bool, msg ...string) bool

ServerError handles server exceptions. Returns true if the condition is true.

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.

func InitResp

func InitResp(ctx *gin.Context) Resp

InitResp initializes a new Resp object from the pool.

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

func (r *Result) To(httpCode ...int)

To sends the Result as a JSON response to the client and releases the object back to the pool.

func (*Result) WithBasic added in v3.0.8

func (r *Result) WithBasic(code int, msg string, data any) Resp

WithBasic sets the basic properties of the Result.

func (*Result) WithContext added in v3.0.8

func (r *Result) WithContext(ctx *gin.Context) Resp

WithContext sets the Gin context for the Result.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳