109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
|
|
package shumai
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
)
|
||
|
|
|
||
|
|
// ShumaiError 数脉服务错误
|
||
|
|
type ShumaiError struct {
|
||
|
|
Code string `json:"code"`
|
||
|
|
Message string `json:"message"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// Error 实现 error 接口
|
||
|
|
func (e *ShumaiError) Error() string {
|
||
|
|
return fmt.Sprintf("数脉错误 [%s]: %s", e.Code, e.Message)
|
||
|
|
}
|
||
|
|
|
||
|
|
// IsSuccess 是否成功
|
||
|
|
func (e *ShumaiError) IsSuccess() bool {
|
||
|
|
return e.Code == "0" || e.Code == "200"
|
||
|
|
}
|
||
|
|
|
||
|
|
// IsNoRecord 是否查无记录
|
||
|
|
func (e *ShumaiError) IsNoRecord() bool {
|
||
|
|
return e.Code == "404"
|
||
|
|
}
|
||
|
|
|
||
|
|
// IsParamError 是否参数错误
|
||
|
|
func (e *ShumaiError) IsParamError() bool {
|
||
|
|
return e.Code == "400"
|
||
|
|
}
|
||
|
|
|
||
|
|
// IsAuthError 是否认证错误
|
||
|
|
func (e *ShumaiError) IsAuthError() bool {
|
||
|
|
return e.Code == "601" || e.Code == "602"
|
||
|
|
}
|
||
|
|
|
||
|
|
// IsSystemError 是否系统错误
|
||
|
|
func (e *ShumaiError) IsSystemError() bool {
|
||
|
|
return e.Code == "500" || e.Code == "501"
|
||
|
|
}
|
||
|
|
|
||
|
|
// 预定义错误
|
||
|
|
var (
|
||
|
|
ErrSuccess = &ShumaiError{Code: "200", Message: "成功"}
|
||
|
|
ErrParamError = &ShumaiError{Code: "400", Message: "参数错误"}
|
||
|
|
ErrNoRecord = &ShumaiError{Code: "404", Message: "请求资源不存在"}
|
||
|
|
ErrSystemError = &ShumaiError{Code: "500", Message: "系统内部错误,请联系服务商"}
|
||
|
|
ErrThirdPartyError = &ShumaiError{Code: "501", Message: "第三方服务异常"}
|
||
|
|
ErrNoPermission = &ShumaiError{Code: "601", Message: "服务商未开通接口权限"}
|
||
|
|
ErrAccountDisabled = &ShumaiError{Code: "602", Message: "账号停用"}
|
||
|
|
ErrInsufficientBalance = &ShumaiError{Code: "603", Message: "余额不足请充值"}
|
||
|
|
ErrInterfaceDisabled = &ShumaiError{Code: "604", Message: "接口停用"}
|
||
|
|
ErrInsufficientQuota = &ShumaiError{Code: "605", Message: "次数不足,请购买套餐"}
|
||
|
|
ErrRateLimitExceeded = &ShumaiError{Code: "606", Message: "调用超限,请联系服务商"}
|
||
|
|
ErrOther = &ShumaiError{Code: "1001", Message: "其他,以实际返回为准"}
|
||
|
|
)
|
||
|
|
|
||
|
|
// NewShumaiError 创建数脉错误
|
||
|
|
func NewShumaiError(code, message string) *ShumaiError {
|
||
|
|
return &ShumaiError{Code: code, Message: message}
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewShumaiErrorFromCode 根据状态码创建错误
|
||
|
|
func NewShumaiErrorFromCode(code string) *ShumaiError {
|
||
|
|
switch code {
|
||
|
|
case "0", "200":
|
||
|
|
return ErrSuccess
|
||
|
|
case "400":
|
||
|
|
return ErrParamError
|
||
|
|
case "404":
|
||
|
|
return ErrNoRecord
|
||
|
|
case "500":
|
||
|
|
return ErrSystemError
|
||
|
|
case "501":
|
||
|
|
return ErrThirdPartyError
|
||
|
|
case "601":
|
||
|
|
return ErrNoPermission
|
||
|
|
case "602":
|
||
|
|
return ErrAccountDisabled
|
||
|
|
case "603":
|
||
|
|
return ErrInsufficientBalance
|
||
|
|
case "604":
|
||
|
|
return ErrInterfaceDisabled
|
||
|
|
case "605":
|
||
|
|
return ErrInsufficientQuota
|
||
|
|
case "606":
|
||
|
|
return ErrRateLimitExceeded
|
||
|
|
case "1001":
|
||
|
|
return ErrOther
|
||
|
|
default:
|
||
|
|
return &ShumaiError{Code: code, Message: "未知错误"}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// IsShumaiError 是否为数脉错误
|
||
|
|
func IsShumaiError(err error) bool {
|
||
|
|
_, ok := err.(*ShumaiError)
|
||
|
|
return ok
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetShumaiError 获取数脉错误
|
||
|
|
func GetShumaiError(err error) *ShumaiError {
|
||
|
|
if e, ok := err.(*ShumaiError); ok {
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|