Documentation
¶
Index ¶
- Constants
- type Error
- func (e *Error) AppendMsg(suffix string) *Error
- func (e *Error) Category() ErrorCategory
- func (e *Error) Code() string
- func (e *Error) Details() map[string]any
- func (e *Error) Error() string
- func (e *Error) HTTPCode() int
- func (e *Error) Message() string
- func (e *Error) PrependMsg(prefix string) *Error
- func (e *Error) Unwrap() error
- func (e *Error) WithDetail(key string, value any) *Error
- func (e *Error) WithDetails(details map[string]any) *Error
- func (e *Error) WithHTTPCode(code int) *Error
- func (e *Error) WithMsg(message string) *Error
- func (e *Error) WithMsgf(format string, args ...any) *Error
- func (e *Error) Wrap(err error) *Error
- type ErrorBuilder
- type ErrorCategory
- type Module
- func (m *Module) AlreadyExists(msg ...string) *Error
- func (m *Module) Code(code string) *ErrorBuilder
- func (m *Module) Conflict(msg ...string) *Error
- func (m *Module) Custom(code string, msg string, httpCode int) *Error
- func (m *Module) Customf(code string, httpCode int, format string, args ...any) *Error
- func (m *Module) Expired(msg ...string) *Error
- func (m *Module) Internal(msg ...string) *Error
- func (m *Module) InvalidParam(msg ...string) *Error
- func (m *Module) InvalidStatus(msg ...string) *Error
- func (m *Module) LimitExceeded(msg ...string) *Error
- func (m *Module) Locked(msg ...string) *Error
- func (m *Module) NotFound(msg ...string) *Error
- func (m *Module) OperationFailed(msg ...string) *Error
- func (m *Module) PermissionDenied(msg ...string) *Error
- func (m *Module) Prefix() string
- func (m *Module) ServiceUnavailable(msg ...string) *Error
- func (m *Module) Unauthorized(msg ...string) *Error
Constants ¶
View Source
const ( // SystemInternalError 系统内部错误 SystemInternalError = "SYSTEM.INTERNAL_ERROR" SystemServiceUnavailable = "SYSTEM.SERVICE_UNAVAILABLE" // SystemTimeout 系统超时 SystemTimeout = "SYSTEM.TIMEOUT" // ValidationFailed 通用验证失败 ValidationFailed = "VALIDATION.FAILED" // ValidationRequired 必填项为空 ValidationRequired = "VALIDATION.REQUIRED" // ValidationMinLength 字符串长度小于最小值 ValidationMinLength = "VALIDATION.MIN_LENGTH" // ValidationMaxLength 字符串长度大于最大值 ValidationMaxLength = "VALIDATION.MAX_LENGTH" // ValidationLength 字符串长度不在指定范围 ValidationLength = "VALIDATION.LENGTH" // ValidationMin 数值小于最小值 ValidationMin = "VALIDATION.MIN" // ValidationMax 数值大于最大值 ValidationMax = "VALIDATION.MAX" // ValidationRange 数值不在指定范围 ValidationRange = "VALIDATION.RANGE" // ValidationEmail 邮箱格式不正确 ValidationEmail = "VALIDATION.EMAIL" // ValidationUrl URL 格式不正确 ValidationUrl = "VALIDATION.URL" // ValidationPattern 不匹配指定正则表达式 ValidationPattern = "VALIDATION.PATTERN" // ValidationIn 值不在枚举列表中 ValidationIn = "VALIDATION.IN" // ValidationNotIn 值在排除列表中 ValidationNotIn = "VALIDATION.NOT_IN" // ValidationNotEmpty 集合不能为空 ValidationNotEmpty = "VALIDATION.NOT_EMPTY" // ValidationMinCount 集合元素数量小于最小值 ValidationMinCount = "VALIDATION.MIN_COUNT" // ValidationMaxCount 集合元素数量大于最大值 ValidationMaxCount = "VALIDATION.MAX_COUNT" // ValidationInvalidInteger 无效的整数 ValidationInvalidInteger = "VALIDATION.INVALID_INTEGER" // ValidationInvalidBoolean 无效的布尔值 ValidationInvalidBoolean = "VALIDATION.INVALID_BOOLEAN" // ValidationInvalidNumber 无效的数字 ValidationInvalidNumber = "VALIDATION.INVALID_NUMBER" // ValidationPositive 必须是正数 ValidationPositive = "VALIDATION.POSITIVE" // ValidationNonNegative 不能是负数 ValidationNonNegative = "VALIDATION.NON_NEGATIVE" // ValidationEnum 枚举值验证失败 ValidationEnum = "VALIDATION.ENUM" // HttpBadRequest 错误的请求 HttpBadRequest = "HTTP.BAD_REQUEST" HttpUnauthorized = "HTTP.UNAUTHORIZED" // HttpForbidden 禁止访问 HttpForbidden = "HTTP.FORBIDDEN" // HttpNotFound 资源不存在 HttpNotFound = "HTTP.NOT_FOUND" // HttpConflict 资源冲突 HttpConflict = "HTTP.CONFLICT" // HttpMethodNotAllowed 方法不允许 HttpMethodNotAllowed = "HTTP.METHOD_NOT_ALLOWED" // AuthTokenExpired 令牌已过期 AuthTokenExpired = "AUTH.TOKEN_EXPIRED" // AuthTokenInvalid 令牌无效 AuthTokenInvalid = "AUTH.TOKEN_INVALID" // AuthPermissionDenied 权限不足 AuthPermissionDenied = "AUTH.PERMISSION_DENIED" // AuthCredentialsInvalid 凭证无效 AuthCredentialsInvalid = "AUTH.CREDENTIALS_INVALID" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error 表示一个结构化的错误
func (*Error) WithDetail ¶
WithDetail 添加单个详细信息(返回新实例)
func (*Error) WithDetails ¶
WithDetails 批量添加详细信息(返回新实例)
func (*Error) WithHTTPCode ¶
WithHTTPCode 设置 HTTP 状态码(返回新实例)
type ErrorBuilder ¶
type ErrorBuilder struct {
// contains filtered or unexported fields
}
ErrorBuilder 错误码构建器
func (*ErrorBuilder) Msg ¶
func (b *ErrorBuilder) Msg(msg string) *Error
Msg 设置错误消息并返回 Error(默认 400 状态码)
func (*ErrorBuilder) MsgWithCode ¶
func (b *ErrorBuilder) MsgWithCode(msg string, httpCode int) *Error
MsgWithCode 设置错误消息和 HTTP 状态码
type ErrorCategory ¶
type ErrorCategory string
ErrorCategory 错误分类
const ( CategoryBusiness ErrorCategory = "BUSINESS" // 业务错误 CategorySystem ErrorCategory = "SYSTEM" // 系统错误 CategoryValidation ErrorCategory = "VALIDATION" // 验证错误 CategoryAuth ErrorCategory = "AUTH" // 认证授权错误 )
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module 表示一个错误模块,提供模块化的错误创建
func (*Module) AlreadyExists ¶
AlreadyExists 资源已存在(409)
func (*Module) Code ¶
func (m *Module) Code(code string) *ErrorBuilder
Code 创建自定义错误码构建器 用法: module.Code("PAYMENT_FAILED").Msg("支付失败")
func (*Module) InvalidParam ¶
InvalidParam 参数无效(400)
func (*Module) InvalidStatus ¶
InvalidStatus 状态无效(400)
func (*Module) LimitExceeded ¶
LimitExceeded 超出限制(429)
func (*Module) OperationFailed ¶
OperationFailed 操作失败(400)
func (*Module) PermissionDenied ¶
PermissionDenied 权限不足(403)
func (*Module) ServiceUnavailable ¶
ServiceUnavailable 服务不可用(503)
func (*Module) Unauthorized ¶
Unauthorized 未授权(401)
Click to show internal directories.
Click to hide internal directories.