response

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CodeDefaultSuccess = NewCode(0, "success", http.StatusOK)
	CodeDefaultFailure = NewCode(500, "failed", http.StatusInternalServerError)

	CodeInvalidParameter = NewCode(400, "invalid parameter", http.StatusBadRequest)
	CodeUnauthorized     = NewCode(401, "unauthorized", http.StatusUnauthorized)
	CodePermissionDeny   = NewCode(403, "permission deny", http.StatusForbidden)
	CodeNotFound         = NewCode(404, "not found", http.StatusNotFound)
	CodeConflict         = NewCode(409, "resource conflict", http.StatusConflict)
	CodeTooManyRequests  = NewCode(429, "too many requests", http.StatusTooManyRequests)
	CodeLocked           = NewCode(423, "resource locked", http.StatusLocked)
	CodeUnImplemented    = NewCode(501, "not implemented", http.StatusNotImplemented)
	CodeBadGateway       = NewCode(502, "bad gateway", http.StatusBadGateway)
	CodeUnavailable      = NewCode(503, "service unavailable", http.StatusServiceUnavailable)
	CodeGatewayTimeout   = NewCode(504, "gateway timeout", http.StatusGatewayTimeout)
)

common code define, using biz code from 0-1000

Functions

func Error

func Error(r *ghttp.Request, err error, data ...interface{})

func Middleware

func Middleware(r *ghttp.Request)

func MiddlewareWithDataHandler

func MiddlewareWithDataHandler(hs ...DataHandler) ghttp.HandlerFunc

func Success

func Success(r *ghttp.Request, data ...interface{})

func WriteData

func WriteData(r *ghttp.Request, ec *Code, data ...interface{})

func WriteJSON

func WriteJSON(r *ghttp.Request, ec *Code)

Types

type Code

type Code struct {
	// contains filtered or unexported fields
}

func CodeFromError

func CodeFromError(err error) *Code

func CodeFromHttpStatus

func CodeFromHttpStatus(status int) *Code

func DefaultFailure

func DefaultFailure() *Code

func DefaultSuccess

func DefaultSuccess() *Code

func NewCode

func NewCode(bizCode int, message string, httpStatus int, detail ...interface{}) *Code

func (*Code) Clone

func (c *Code) Clone() *Code

func (*Code) Code

func (c *Code) Code() int

func (*Code) Detail

func (c *Code) Detail() interface{}

func (*Code) Error

func (c *Code) Error() string

func (*Code) Message

func (c *Code) Message() string

func (*Code) Status

func (c *Code) Status() int

func (*Code) WithCode

func (c *Code) WithCode(code int) *Code

func (*Code) WithDetail

func (c *Code) WithDetail(detail interface{}) *Code

func (*Code) WithHttpStatus

func (c *Code) WithHttpStatus(status int) *Code

func (*Code) WithMessage

func (c *Code) WithMessage(message string) *Code

type DataHandler

type DataHandler = func(ctx context.Context, res any) any

type JSON

type JSON struct {
	Message string      `json:"message"`
	Code    int         `json:"code"`
	Data    interface{} `json:"data,omitempty"`
	TraceId string      `json:"trace_id,omitempty"`
}

Jump to

Keyboard shortcuts

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