Files
tyapi-server/internal/infrastructure/external/jiguang/jiguang_errors.go

150 lines
4.2 KiB
Go
Raw Normal View History

package jiguang
import (
"fmt"
)
// JiguangError 极光服务错误
type JiguangError struct {
Code int `json:"code"`
Message string `json:"message"`
}
// Error 实现error接口
func (e *JiguangError) Error() string {
return fmt.Sprintf("极光错误 [%d]: %s", e.Code, e.Message)
}
// IsSuccess 检查是否成功
func (e *JiguangError) IsSuccess() bool {
return e.Code == 0
}
// IsQueryFailed 检查是否查询失败
func (e *JiguangError) IsQueryFailed() bool {
return e.Code == 922
}
// IsNoRecord 检查是否查无记录
func (e *JiguangError) IsNoRecord() bool {
return e.Code == 921
}
// IsParamError 检查是否是参数相关错误
func (e *JiguangError) IsParamError() bool {
return e.Code == 400 || e.Code == 906 || e.Code == 914 || e.Code == 918
}
// IsAuthError 检查是否是认证相关错误
func (e *JiguangError) IsAuthError() bool {
return e.Code == 902 || e.Code == 903 || e.Code == 904 || e.Code == 905
}
// IsSystemError 检查是否是系统错误
func (e *JiguangError) IsSystemError() bool {
return e.Code == 405 || e.Code == 911 || e.Code == 912 || e.Code == 915 || e.Code == 916 || e.Code == 917 || e.Code == 919 || e.Code == 923
}
// 预定义错误常量
var (
// 成功状态
ErrSuccess = &JiguangError{Code: 0, Message: "请求成功"}
// 参数错误
ErrParamInvalid = &JiguangError{Code: 400, Message: "请求参数不正确"}
ErrMethodInvalid = &JiguangError{Code: 405, Message: "请求方法不正确"}
ErrParamFormInvalid = &JiguangError{Code: 906, Message: "请求参数形式不正确"}
ErrBodyIncomplete = &JiguangError{Code: 914, Message: "Body 请求参数不完整"}
ErrBodyNotSupported = &JiguangError{Code: 918, Message: "Body 请求参数不支持"}
// 认证错误
ErrAppIDInvalid = &JiguangError{Code: 902, Message: "错误的 appId/账户已删除"}
ErrTimestampInvalid = &JiguangError{Code: 903, Message: "错误的时间戳/时间误差大于 10 分钟"}
ErrSignMethodInvalid = &JiguangError{Code: 904, Message: "无法识别的签名方法"}
ErrSignInvalid = &JiguangError{Code: 905, Message: "签名不合法"}
// 系统错误
ErrAccountStatusError = &JiguangError{Code: 911, Message: "账户状态异常"}
ErrInterfaceDisabled = &JiguangError{Code: 912, Message: "接口状态不可用"}
ErrAPICallError = &JiguangError{Code: 915, Message: "API 接口调用有误"}
ErrInternalError = &JiguangError{Code: 916, Message: "内部接口调用错误,请联系相关人员"}
ErrTimeout = &JiguangError{Code: 917, Message: "请求超时"}
ErrBusinessDisabled = &JiguangError{Code: 919, Message: "业务状态不可用"}
ErrInterfaceException = &JiguangError{Code: 923, Message: "接口异常"}
// 业务错误
ErrNoRecord = &JiguangError{Code: 921, Message: "查无记录"}
ErrQueryFailed = &JiguangError{Code: 922, Message: "查询失败"}
)
// NewJiguangError 创建新的极光错误
func NewJiguangError(code int, message string) *JiguangError {
return &JiguangError{
Code: code,
Message: message,
}
}
// NewJiguangErrorFromCode 根据状态码创建错误
func NewJiguangErrorFromCode(code int) *JiguangError {
switch code {
case 0:
return ErrSuccess
case 400:
return ErrParamInvalid
case 405:
return ErrMethodInvalid
case 902:
return ErrAppIDInvalid
case 903:
return ErrTimestampInvalid
case 904:
return ErrSignMethodInvalid
case 905:
return ErrSignInvalid
case 906:
return ErrParamFormInvalid
case 911:
return ErrAccountStatusError
case 912:
return ErrInterfaceDisabled
case 914:
return ErrBodyIncomplete
case 915:
return ErrAPICallError
case 916:
return ErrInternalError
case 917:
return ErrTimeout
case 918:
return ErrBodyNotSupported
case 919:
return ErrBusinessDisabled
case 921:
return ErrNoRecord
case 922:
return ErrQueryFailed
case 923:
return ErrInterfaceException
default:
return &JiguangError{
Code: code,
Message: fmt.Sprintf("未知错误码: %d", code),
}
}
}
// IsJiguangError 检查是否是极光错误
func IsJiguangError(err error) bool {
_, ok := err.(*JiguangError)
return ok
}
// GetJiguangError 获取极光错误
func GetJiguangError(err error) *JiguangError {
if jiguangErr, ok := err.(*JiguangError); ok {
return jiguangErr
}
return nil
}