Documentation
¶
Index ¶
- Constants
- func IsApiException(err error, code int) bool
- func IsConflictError(err error) bool
- func IsNotFoundError(err error) bool
- type ApiException
- func NewAPIExceptionFromError(err error) *ApiException
- func NewAccessTokenExpired(format string, a ...interface{}) *ApiException
- func NewAccessTokenIllegal(format string, a ...interface{}) *ApiException
- func NewApiException(code int, reason string) *ApiException
- func NewApiExceptionFromString(msg string) *ApiException
- func NewBadRequest(format string, a ...interface{}) *ApiException
- func NewConflict(format string, a ...interface{}) *ApiException
- func NewInternalServerError(format string, a ...interface{}) *ApiException
- func NewNotFound(format string, a ...interface{}) *ApiException
- func NewOtherClientsLoggedIn(format string, a ...interface{}) *ApiException
- func NewOtherIPLoggedIn(format string, a ...interface{}) *ApiException
- func NewOtherPlaceLoggedIn(format string, a ...interface{}) *ApiException
- func NewPasswordExired(format string, a ...interface{}) *ApiException
- func NewPermissionDeny(format string, a ...interface{}) *ApiException
- func NewRefreshTokenExpired(format string, a ...interface{}) *ApiException
- func NewRefreshTokenIllegal(format string, a ...interface{}) *ApiException
- func NewSessionTerminated(format string, a ...interface{}) *ApiException
- func NewUnauthorized(format string, a ...interface{}) *ApiException
- func NewVerifyCodeRequiredError(format string, a ...interface{}) *ApiException
- func Wrap(err error, code int, reason string) *ApiException
- func Wrapf(err error, code int, reason string, format string, args ...any) *ApiException
- func (e *ApiException) Cause() error
- func (e *ApiException) Error() string
- func (e *ApiException) ErrorChain() []string
- func (e *ApiException) ErrorChainString() string
- func (e *ApiException) ErrorCode() int
- func (e *ApiException) GetData() interface{}
- func (e *ApiException) GetHttpCode() int
- func (e *ApiException) GetMeta(key string) any
- func (e *ApiException) GetNamespace() string
- func (e *ApiException) GetReason() string
- func (e *ApiException) GetRequestID() string
- func (e *ApiException) GetStack() string
- func (e *ApiException) GetTraceID() string
- func (e *ApiException) Is(t error) bool
- func (e *ApiException) IsAuthError() bool
- func (e *ApiException) IsClientError() bool
- func (e *ApiException) IsRetryable() bool
- func (e *ApiException) IsServerError() bool
- func (e *ApiException) IsTemporary() bool
- func (e *ApiException) ToJson() string
- func (e *ApiException) Type() ErrorType
- func (e *ApiException) Unwrap() error
- func (e *ApiException) WithCause(cause error) *ApiException
- func (e *ApiException) WithData(d any) *ApiException
- func (e *ApiException) WithHttpCode(httpCode int) *ApiException
- func (e *ApiException) WithMessage(m string) *ApiException
- func (e *ApiException) WithMessagef(format string, a ...any) *ApiException
- func (e *ApiException) WithMeta(key string, value any) *ApiException
- func (e *ApiException) WithNamespace(ns string) *ApiException
- func (e *ApiException) WithRequestID(requestID string) *ApiException
- func (e *ApiException) WithStack() *ApiException
- func (e *ApiException) WithStackDepth(depth int) *ApiException
- func (e *ApiException) WithTraceID(traceID string) *ApiException
- type ErrorType
- type ExceptionBuilder
- func (b *ExceptionBuilder) Build() *ApiException
- func (b *ExceptionBuilder) WithCause(cause error) *ExceptionBuilder
- func (b *ExceptionBuilder) WithData(data any) *ExceptionBuilder
- func (b *ExceptionBuilder) WithHTTPCode(httpCode int) *ExceptionBuilder
- func (b *ExceptionBuilder) WithMessage(message string) *ExceptionBuilder
- func (b *ExceptionBuilder) WithMessagef(format string, args ...any) *ExceptionBuilder
- func (b *ExceptionBuilder) WithMeta(key string, value any) *ExceptionBuilder
- func (b *ExceptionBuilder) WithReason(reason string) *ExceptionBuilder
- func (b *ExceptionBuilder) WithRequestID(requestID string) *ExceptionBuilder
- func (b *ExceptionBuilder) WithService(service string) *ExceptionBuilder
- func (b *ExceptionBuilder) WithStack() *ExceptionBuilder
- func (b *ExceptionBuilder) WithTraceID(traceID string) *ExceptionBuilder
Constants ¶
const ( // CODE_OTHER_PLACE_LGOIN 登录登录 CODE_OTHER_PLACE_LGOIN = 50010 // CODE_OTHER_IP_LOGIN 异常IP登录 CODE_OTHER_IP_LOGIN = 50011 // CODE_OTHER_CLIENT_LOGIN 用户已经通过其他端登录 CODE_OTHER_CLIENT_LOGIN = 50012 // CODE_SESSION_TERMINATED 会话中断 CODE_SESSION_TERMINATED = 50013 // CODE_ACESS_TOKEN_EXPIRED token过期 CODE_ACESS_TOKEN_EXPIRED = 50014 // CODE_REFRESH_TOKEN_EXPIRED token过期 CODE_REFRESH_TOKEN_EXPIRED = 50015 // CODE_ACCESS_TOKEN_ILLEGAL 访问token不合法 CODE_ACCESS_TOKEN_ILLEGAL = 50016 // CODE_REFRESH_TOKEN_ILLEGAL 刷新token不合法 CODE_REFRESH_TOKEN_ILLEGAL = 50017 // CODE_VERIFY_CODE_REQUIRED 需要验证码 CODE_VERIFY_CODE_REQUIRED = 50018 // CODE_PASSWORD_EXPIRED 用户密码过期 CODE_PASSWORD_EXPIRED = 50019 // CODE_UNAUTHORIZED 未认证 CODE_UNAUTHORIZED = http.StatusUnauthorized // CODE_BAD_REQUEST 请求不合法 CODE_BAD_REQUEST = http.StatusBadRequest // CODE_INTERNAL_SERVER_ERROR 服务端内部错误 CODE_INTERNAL_SERVER_ERROR = http.StatusInternalServerError // CODE_FORBIDDEN 无权限 CODE_FORBIDDEN = http.StatusForbidden // CODE_NOT_FOUND 接口未找到 CODE_NOT_FOUND = http.StatusNotFound // CODE_CONFLICT 资源冲突, 已经存在 CODE_CONFLICT = http.StatusConflict // CODE_UNKNOWN 未知异常 CODE_UNKNOWN = 99999 )
const (
// GRPC Trailer 异常转换时定义的key名称
TRAILER_ERROR_JSON_KEY = "err_json"
)
Variables ¶
This section is empty.
Functions ¶
func IsApiException ¶ added in v2.0.27
Types ¶
type ApiException ¶ added in v2.0.27
type ApiException struct {
Service string `json:"service"`
TraceID string `json:"trace_id,omitempty"` // 追踪ID
RequestID string `json:"request_id,omitempty"` // 请求ID
HttpCode int `json:"http_code,omitempty"`
Code int `json:"code"`
Reason string `json:"reason"`
Message string `json:"message"`
Meta map[string]any `json:"meta"`
Data any `json:"data"`
// contains filtered or unexported fields
}
ApiException API异常
func NewAPIExceptionFromError ¶
func NewAPIExceptionFromError(err error) *ApiException
{"namespace":"","http_code":404,"error_code":404,"reason":"资源未找到","message":"test","meta":null,"data":null}
func NewAccessTokenExpired ¶
func NewAccessTokenExpired(format string, a ...interface{}) *ApiException
NewAccessTokenExpired 访问token过期
func NewAccessTokenIllegal ¶
func NewAccessTokenIllegal(format string, a ...interface{}) *ApiException
NewAccessTokenIllegal 访问token过期
func NewApiException ¶ added in v2.0.27
func NewApiException(code int, reason string) *ApiException
NewApiException 创建一个API异常 用于其他模块自定义异常
func NewApiExceptionFromString ¶ added in v2.0.27
func NewApiExceptionFromString(msg string) *ApiException
func NewBadRequest ¶
func NewBadRequest(format string, a ...interface{}) *ApiException
NewBadRequest todo
func NewInternalServerError ¶
func NewInternalServerError(format string, a ...interface{}) *ApiException
NewInternalServerError 500
func NewOtherClientsLoggedIn ¶
func NewOtherClientsLoggedIn(format string, a ...interface{}) *ApiException
NewOtherClientsLoggedIn 其他端登录
func NewOtherIPLoggedIn ¶
func NewOtherIPLoggedIn(format string, a ...interface{}) *ApiException
NewOtherIPLoggedIn 异常IP登录
func NewOtherPlaceLoggedIn ¶
func NewOtherPlaceLoggedIn(format string, a ...interface{}) *ApiException
NewOtherPlaceLoggedIn 异地登录
func NewPasswordExired ¶
func NewPasswordExired(format string, a ...interface{}) *ApiException
NewPasswordExired 50019
func NewPermissionDeny ¶
func NewPermissionDeny(format string, a ...interface{}) *ApiException
NewPermissionDeny 没有权限访问
func NewRefreshTokenExpired ¶
func NewRefreshTokenExpired(format string, a ...interface{}) *ApiException
NewRefreshTokenExpired 刷新token过期
func NewRefreshTokenIllegal ¶
func NewRefreshTokenIllegal(format string, a ...interface{}) *ApiException
NewRefreshTokenIllegal 访问token过期
func NewSessionTerminated ¶
func NewSessionTerminated(format string, a ...interface{}) *ApiException
NewSessionTerminated 会话结束
func NewUnauthorized ¶
func NewUnauthorized(format string, a ...interface{}) *ApiException
NewUnauthorized 未认证
func NewVerifyCodeRequiredError ¶
func NewVerifyCodeRequiredError(format string, a ...interface{}) *ApiException
NewVerifyCodeRequiredError 50018
func Wrap ¶ added in v2.1.0
func Wrap(err error, code int, reason string) *ApiException
Wrap 包装一个错误,支持错误链追踪 这是推荐的创建异常的方式,可以保留原始错误信息
func (*ApiException) Error ¶ added in v2.0.27
func (e *ApiException) Error() string
func (*ApiException) ErrorChain ¶ added in v2.1.0
func (e *ApiException) ErrorChain() []string
ErrorChain 返回完整的错误链信息
func (*ApiException) ErrorChainString ¶ added in v2.1.0
func (e *ApiException) ErrorChainString() string
ErrorChainString 返回格式化的错误链字符串
func (*ApiException) ErrorCode ¶ added in v2.0.27
func (e *ApiException) ErrorCode() int
Code exception's code, 如果code不存在返回-1
func (*ApiException) GetData ¶ added in v2.0.27
func (e *ApiException) GetData() interface{}
func (*ApiException) GetHttpCode ¶ added in v2.0.27
func (e *ApiException) GetHttpCode() int
Code exception's code, 如果code不存在返回-1
func (*ApiException) GetMeta ¶ added in v2.0.27
func (e *ApiException) GetMeta(key string) any
func (*ApiException) GetNamespace ¶ added in v2.0.27
func (e *ApiException) GetNamespace() string
func (*ApiException) GetReason ¶ added in v2.0.27
func (e *ApiException) GetReason() string
func (*ApiException) GetRequestID ¶ added in v2.1.0
func (e *ApiException) GetRequestID() string
GetRequestID 获取请求ID
func (*ApiException) GetStack ¶ added in v2.1.0
func (e *ApiException) GetStack() string
GetStack 获取堆栈信息
func (*ApiException) GetTraceID ¶ added in v2.1.0
func (e *ApiException) GetTraceID() string
GetTraceID 获取追踪ID
func (*ApiException) Is ¶ added in v2.0.27
func (e *ApiException) Is(t error) bool
func (*ApiException) IsAuthError ¶ added in v2.1.0
func (e *ApiException) IsAuthError() bool
IsAuthError 判断是否是认证/授权错误
func (*ApiException) IsClientError ¶ added in v2.1.0
func (e *ApiException) IsClientError() bool
IsClientError 判断是否是客户端错误(4xx)
func (*ApiException) IsRetryable ¶ added in v2.1.0
func (e *ApiException) IsRetryable() bool
IsRetryable 判断错误是否可重试
func (*ApiException) IsServerError ¶ added in v2.1.0
func (e *ApiException) IsServerError() bool
IsServerError 判断是否是服务端错误(5xx)
func (*ApiException) IsTemporary ¶ added in v2.1.0
func (e *ApiException) IsTemporary() bool
IsTemporary 判断是否是临时错误
func (*ApiException) ToJson ¶ added in v2.0.27
func (e *ApiException) ToJson() string
func (*ApiException) Unwrap ¶ added in v2.1.0
func (e *ApiException) Unwrap() error
Unwrap 返回包装的原始错误,支持 Go 1.13+ 错误链
func (*ApiException) WithCause ¶ added in v2.1.0
func (e *ApiException) WithCause(cause error) *ApiException
WithCause 设置原始错误
func (*ApiException) WithData ¶ added in v2.0.27
func (e *ApiException) WithData(d any) *ApiException
func (*ApiException) WithHttpCode ¶ added in v2.0.27
func (e *ApiException) WithHttpCode(httpCode int) *ApiException
func (*ApiException) WithMessage ¶ added in v2.0.27
func (e *ApiException) WithMessage(m string) *ApiException
func (*ApiException) WithMessagef ¶ added in v2.0.27
func (e *ApiException) WithMessagef(format string, a ...any) *ApiException
func (*ApiException) WithMeta ¶ added in v2.0.27
func (e *ApiException) WithMeta(key string, value any) *ApiException
WithMeta 携带一些额外信息(线程安全)
func (*ApiException) WithNamespace ¶ added in v2.0.27
func (e *ApiException) WithNamespace(ns string) *ApiException
func (*ApiException) WithRequestID ¶ added in v2.1.0
func (e *ApiException) WithRequestID(requestID string) *ApiException
WithRequestID 设置请求ID
func (*ApiException) WithStack ¶ added in v2.1.0
func (e *ApiException) WithStack() *ApiException
WithStack 添加堆栈追踪信息(默认3层)
func (*ApiException) WithStackDepth ¶ added in v2.1.0
func (e *ApiException) WithStackDepth(depth int) *ApiException
WithStackDepth 添加指定层数的堆栈追踪
func (*ApiException) WithTraceID ¶ added in v2.1.0
func (e *ApiException) WithTraceID(traceID string) *ApiException
WithTraceID 设置追踪ID
type ExceptionBuilder ¶ added in v2.1.0
type ExceptionBuilder struct {
// contains filtered or unexported fields
}
ExceptionBuilder 异常构建器,提供更灵活的异常创建方式
func (*ExceptionBuilder) Build ¶ added in v2.1.0
func (b *ExceptionBuilder) Build() *ApiException
Build 构建API异常
func (*ExceptionBuilder) WithCause ¶ added in v2.1.0
func (b *ExceptionBuilder) WithCause(cause error) *ExceptionBuilder
WithCause 设置原始错误
func (*ExceptionBuilder) WithData ¶ added in v2.1.0
func (b *ExceptionBuilder) WithData(data any) *ExceptionBuilder
WithData 设置响应数据
func (*ExceptionBuilder) WithHTTPCode ¶ added in v2.1.0
func (b *ExceptionBuilder) WithHTTPCode(httpCode int) *ExceptionBuilder
WithHTTPCode 设置HTTP状态码
func (*ExceptionBuilder) WithMessage ¶ added in v2.1.0
func (b *ExceptionBuilder) WithMessage(message string) *ExceptionBuilder
WithMessage 设置错误消息
func (*ExceptionBuilder) WithMessagef ¶ added in v2.1.0
func (b *ExceptionBuilder) WithMessagef(format string, args ...any) *ExceptionBuilder
WithMessagef 设置格式化的错误消息
func (*ExceptionBuilder) WithMeta ¶ added in v2.1.0
func (b *ExceptionBuilder) WithMeta(key string, value any) *ExceptionBuilder
WithMeta 添加元数据
func (*ExceptionBuilder) WithReason ¶ added in v2.1.0
func (b *ExceptionBuilder) WithReason(reason string) *ExceptionBuilder
WithReason 设置错误原因
func (*ExceptionBuilder) WithRequestID ¶ added in v2.1.0
func (b *ExceptionBuilder) WithRequestID(requestID string) *ExceptionBuilder
WithRequestID 设置请求ID
func (*ExceptionBuilder) WithService ¶ added in v2.1.0
func (b *ExceptionBuilder) WithService(service string) *ExceptionBuilder
WithService 设置服务名称
func (*ExceptionBuilder) WithStack ¶ added in v2.1.0
func (b *ExceptionBuilder) WithStack() *ExceptionBuilder
WithStack 启用堆栈追踪
func (*ExceptionBuilder) WithTraceID ¶ added in v2.1.0
func (b *ExceptionBuilder) WithTraceID(traceID string) *ExceptionBuilder
WithTraceID 设置追踪ID