Documentation
¶
Index ¶
- Constants
- func CodeReason(code int) string
- 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 NewApiExceptionWithCause(code int, reason string, cause error) *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 (e *ApiException) Error() 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) Is(t error) bool
- func (e *ApiException) ToJson() string
- func (e *ApiException) Unwrap() error
- func (e *ApiException) WithData(d any) *ApiException
- func (e *ApiException) WithHttpCode(httpCode int) *ApiException
- func (e *ApiException) WithLocationFromCaller(extraSkip 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
- type ErrorLocation
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 ¶
Types ¶
type ApiException ¶
type ApiException struct {
Service string `json:"service"`
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"`
// 最初报错点信息
Location *ErrorLocation `json:"location,omitempty"`
// Cause 用于保留原始错误,便于错误链追踪(不会序列化到JSON)
Cause error `json:"-"`
}
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 ¶
func NewApiException(code int, reason string) *ApiException
NewApiException 创建一个API异常 用于其他模块自定义异常
func NewApiExceptionFromString ¶
func NewApiExceptionFromString(msg string) *ApiException
func NewApiExceptionWithCause ¶
func NewApiExceptionWithCause(code int, reason string, cause error) *ApiException
NewApiExceptionWithCause 创建带原始cause的API异常,便于错误链追踪
func NewBadRequest ¶
func NewBadRequest(format string, a ...interface{}) *ApiException
NewBadRequest 创建请求参数错误异常 适用于:参数验证失败、请求格式错误、必填参数缺失等场景
func NewConflict ¶
func NewConflict(format string, a ...interface{}) *ApiException
NewConflict 创建资源冲突异常 适用于:创建重复资源、数据唯一性冲突、并发操作冲突等场景
func NewInternalServerError ¶
func NewInternalServerError(format string, a ...interface{}) *ApiException
NewInternalServerError 500
func NewNotFound ¶
func NewNotFound(format string, a ...interface{}) *ApiException
NewNotFound 创建资源未找到异常 适用于:查询不存在的资源、接口路径不存在、文件不存在等场景
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 (*ApiException) Error ¶
func (e *ApiException) Error() string
func (*ApiException) ErrorCode ¶
func (e *ApiException) ErrorCode() int
Code exception's code, 如果code不存在返回-1
func (*ApiException) GetData ¶
func (e *ApiException) GetData() interface{}
func (*ApiException) GetHttpCode ¶
func (e *ApiException) GetHttpCode() int
Code exception's code, 如果code不存在返回-1
func (*ApiException) GetMeta ¶
func (e *ApiException) GetMeta(key string) any
func (*ApiException) GetNamespace ¶
func (e *ApiException) GetNamespace() string
func (*ApiException) GetReason ¶
func (e *ApiException) GetReason() string
func (*ApiException) Is ¶
func (e *ApiException) Is(t error) bool
func (*ApiException) ToJson ¶
func (e *ApiException) ToJson() string
func (*ApiException) Unwrap ¶
func (e *ApiException) Unwrap() error
Unwrap 允许errors.Is / errors.As 继续向下查找底层错误
func (*ApiException) WithData ¶
func (e *ApiException) WithData(d any) *ApiException
func (*ApiException) WithHttpCode ¶
func (e *ApiException) WithHttpCode(httpCode int) *ApiException
func (*ApiException) WithLocationFromCaller ¶ added in v1.2.34
func (e *ApiException) WithLocationFromCaller(extraSkip int) *ApiException
WithLocationFromCaller 允许调用方指定“再往上跳几层”来记录位置。 baseSkip 由库内部控制(跳过异常包自身),extraSkip 由使用方指定。
func (*ApiException) WithMessage ¶
func (e *ApiException) WithMessage(m string) *ApiException
func (*ApiException) WithMessagef ¶
func (e *ApiException) WithMessagef(format string, a ...any) *ApiException
func (*ApiException) WithMeta ¶
func (e *ApiException) WithMeta(key string, value any) *ApiException
WithMeta 携带一些额外信息
func (*ApiException) WithNamespace ¶
func (e *ApiException) WithNamespace(ns string) *ApiException
type ErrorLocation ¶ added in v1.2.34
type ErrorLocation struct {
File string `json:"file,omitempty"` // 源文件路径
Line int `json:"line,omitempty"` // 行号
Func string `json:"func,omitempty"` // 函数/方法名(含包名、接收者)
}
ErrorLocation 表示最初报错的位置