context

package
v0.0.0-...-47adacd Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SUCCESS_OK              = 100000 // 操作成功
	SUCCESS_NO_CONTENT      = 100001 // 成功但无返回内容
	SUCCESS_ACCEPTED        = 100002 // 请求已接受
	SUCCESS_PARTIAL_CONTENT = 100003 // 部分内容
)

成功类

View Source
const (
	CLIENT_PARAM_ERROR       = 200001 // 参数错误
	CLIENT_NOT_FOUND         = 200002 // 资源不存在
	CLIENT_UNAUTHORIZED      = 200003 // 未认证
	CLIENT_FORBIDDEN         = 200004 // 禁止访问
	CLIENT_CONFLICT          = 200005 // 资源冲突
	CLIENT_TOO_MANY_REQUESTS = 200006 // 请求频率过高
	CLIENT_INVALID_TOKEN     = 200007 // 无效令牌
	CLIENT_TOKEN_EXPIRED     = 200008 // 令牌过期
	CLIENT_UNSUPPORTED_MEDIA = 200009 // 不支持的媒体类型
	CLIENT_VALIDATION_FAILED = 200010 // 数据验证失败
	CLIENT_MISSING_HEADER    = 200011 // 缺少必要请求头
	CLIENT_INVALID_FORMAT    = 200012 // 格式错误
)

客户端错误类

View Source
const (
	SERVER_INTERNAL_ERROR      = 300001 // 服务端内部错误
	SERVER_DATABASE_ERROR      = 300002 // 数据库操作失败
	SERVER_REDIS_ERROR         = 300003 // Redis 操作失败
	SERVER_RATE_LIMIT          = 300004 // 接口限流
	SERVER_SERVICE_UNAVAILABLE = 300005 // 服务不可用
	SERVER_TIMEOUT             = 300006 // 操作超时
	SERVER_CONFIG_ERROR        = 300007 // 配置错误
	SERVER_INIT_FAILED         = 300008 // 初始化失败
	BUSINESS_ERROR             = 310000 // 业务错误
)

服务端错误类

View Source
const (
	THIRD_PARTY_ERROR         = 400001 // 第三方服务错误
	THIRD_PARTY_PAYMENT_ERROR = 400002 // 支付服务错误
	THIRD_PARTY_SMS_ERROR     = 400003 // 短信服务错误
	THIRD_PARTY_EMAIL_ERROR   = 400004 // 邮件服务错误
	THIRD_PARTY_STORAGE_ERROR = 400005 // 存储服务错误
	THIRD_PARTY_API_ERROR     = 400006 // API调用错误
)

第三方服务错误类

View Source
const (
	SYSTEM_ERROR              = 500001 // 系统错误
	SYSTEM_RESOURCE_EXHAUSTED = 500002 // 资源耗尽
	SYSTEM_FILE_NOT_FOUND     = 500003 // 文件不存在
	SYSTEM_PERMISSION_DENIED  = 500004 // 权限不足
)

系统错误类

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	*app.RequestContext
	// contains filtered or unexported fields
}

Context 是对 Hertz 的 app.RequestContext 的封装

func NewContext

func NewContext(ctx context.Context, c *app.RequestContext) *Context

NewContext 创建一个新的 Context

func (*Context) Abort

func (ctx *Context) Abort()

Abort 阻止待处理的处理程序被调用

func (*Context) AbortWithStatus

func (ctx *Context) AbortWithStatus(code int)

AbortWithStatus 调用 Abort 并写入指定的状态码

func (*Context) Bind

func (ctx *Context) Bind(obj any) error

Bind 将请求数据绑定到给定的结构体指针

func (*Context) ClientIP

func (ctx *Context) ClientIP() string

ClientIP 返回客户端的 IP 地址

func (*Context) ContentType

func (ctx *Context) ContentType() string

ContentType 返回请求的 Content-Type 头

func (*Context) Context

func (ctx *Context) Context() context.Context

Context 返回原始的 context.Context

func (*Context) Cookie

func (ctx *Context) Cookie(name string) string

Cookie 返回请求中指定名称的 cookie 值

func (*Context) Data

func (ctx *Context) Data(code int, contentType string, data []byte)

Data 将一些数据写入响应体并更新 HTTP 状态码

func (*Context) Error

func (ctx *Context) Error(err error)

Error 将错误附加到当前上下文

func (*Context) File

func (ctx *Context) File(filepath string)

File 将指定文件写入响应体

func (*Context) FormFile

func (ctx *Context) FormFile(name string) (*multipart.FileHeader, error)

FormFile 返回指定表单键的第一个文件

func (*Context) FullPath

func (ctx *Context) FullPath() string

FullPath 返回匹配的路由完整路径

func (*Context) GetHeader

func (ctx *Context) GetHeader(key string) string

GetHeader 获取请求头

func (*Context) GetRawData

func (ctx *Context) GetRawData() []byte

GetRawData 获取原始请求体

func (*Context) GetResponseHeader

func (ctx *Context) GetResponseHeader(key string) string

GetResponseHeader 获取响应头

func (*Context) GetUserID

func (ctx *Context) GetUserID() uint64

func (*Context) GetUserValue

func (ctx *Context) GetUserValue(key string) (value any, exists bool)

GetUserValue 从用户值中获取指定键的值

func (*Context) HTML

func (ctx *Context) HTML(code int, name string, obj any)

HTML 渲染 HTML 模板

func (*Context) IsAborted

func (ctx *Context) IsAborted() bool

IsAborted 返回当前上下文是否已经被终止

func (*Context) IsWebsocket

func (ctx *Context) IsWebsocket() bool

IsWebsocket 如果请求头表明客户端正在发起 websocket 握手,则返回 true

func (*Context) JSON

func (ctx *Context) JSON(code int, obj any)

JSON 将给定的结构体序列化为 JSON 并写入响应体

func (*Context) Method

func (ctx *Context) Method() string

Method 返回请求的 HTTP 方法

func (*Context) Param

func (ctx *Context) Param(key string) string

Param 返回 URL 参数的值

func (*Context) Path

func (ctx *Context) Path() string

Path 返回请求的路径

func (*Context) PostForm

func (ctx *Context) PostForm(key string) string

PostForm 返回 POST 表单中指定键的值

func (*Context) Query

func (ctx *Context) Query(key string) string

Query 返回 URL 查询参数的值

func (*Context) Redirect

func (ctx *Context) Redirect(code int, location string)

Redirect 重定向请求到指定的 URL

func (*Context) RequestBody

func (ctx *Context) RequestBody() []byte

RequestBody 返回请求体

func (*Context) SaveUploadedFile

func (ctx *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error

SaveUploadedFile 将上传的文件保存到指定目标

func (*Context) SetCookie

func (ctx *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)

SetCookie 添加一个 Set-Cookie 头到响应头中

func (*Context) SetCookieKV

func (ctx *Context) SetCookieKV(key, value string)

SetCookieKV 设置 cookie(简化版)

func (*Context) SetHeader

func (ctx *Context) SetHeader(key, value string)

SetHeader 设置响应头

func (*Context) SetStatusCode

func (ctx *Context) SetStatusCode(statusCode int)

SetStatusCode 设置响应状态码

func (*Context) SetUserValue

func (ctx *Context) SetUserValue(key string, value any)

SetUserValue 设置用户值

func (*Context) Status

func (ctx *Context) Status(code int)

Status 设置 HTTP 响应状态码

func (*Context) String

func (ctx *Context) String(code int, format string, values ...any)

String 将给定的字符串写入响应体

func (*Context) Write

func (ctx *Context) Write(data []byte) (int, error)

Write 将字节切片写入响应

func (*Context) WriteString

func (ctx *Context) WriteString(s string) (int, error)

WriteString 将字符串写入响应

type Response

type Response struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data"`
}

func BusinessError

func BusinessError(message ...any) *Response

func Conflict

func Conflict(message any) *Response

Conflict 响应资源冲突错误(如重复创建)

func DatabaseError

func DatabaseError(message any) *Response

func InternalError

func InternalError(message ...any) *Response

服务端错误响应函数

func NoContent

func NoContent() *Response

NoContent 响应成功但无内容(如 DELETE 请求)

func NotFound

func NotFound(message any) *Response

func PageSuccess

func PageSuccess(data any, total int64) *Response

func ParamError

func ParamError(message any) *Response

客户端错误响应函数(支持string/error类型)

func PaymentError

func PaymentError(message any) *Response

func RateLimit

func RateLimit(message any) *Response

接口限流响应函数

func Success

func Success(data any) *Response

成功响应函数

func SystemError

func SystemError(message any) *Response

系统错误响应函数

func ThirdPartyError

func ThirdPartyError(serviceName string, message any) *Response

第三方服务错误响应函数

func Unauthorized

func Unauthorized(message any) *Response

func (*Response) Write

func (rsp *Response) Write(ctx *Context)

Jump to

Keyboard shortcuts

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