76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package rongxing
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
)
|
||
|
||
// 业务返回码(仅用于错误描述/分类,不作为扣费依据)
|
||
const (
|
||
CodeSuccess = "200" // 成功
|
||
CodeNoData = "204" // 未查到数据
|
||
CodeInternalErr = "503" // 内部服务错误
|
||
)
|
||
|
||
// consumeFlag 扣费标记(对方各业务码场景下均可能返回)
|
||
const (
|
||
ConsumeFlagNoCharge = 0 // 不扣费
|
||
ConsumeFlagCharge = 1 // 扣费
|
||
)
|
||
|
||
var (
|
||
ErrDatasource = errors.New("数据源异常")
|
||
ErrSystem = errors.New("系统异常")
|
||
ErrNotFound = errors.New("查询为空")
|
||
)
|
||
|
||
var codeMessage = map[string]string{
|
||
CodeSuccess: "成功",
|
||
CodeNoData: "未查到数据",
|
||
CodeInternalErr: "内部服务错误",
|
||
}
|
||
|
||
// GetCodeMessage 返回码描述
|
||
func GetCodeMessage(code string) string {
|
||
if msg, ok := codeMessage[code]; ok {
|
||
return msg
|
||
}
|
||
return ""
|
||
}
|
||
|
||
// IsBillable 按 consumeFlag 判断是否扣费(不以 code 为依据)
|
||
func IsBillable(consumeFlag int) bool {
|
||
return consumeFlag == ConsumeFlagCharge
|
||
}
|
||
|
||
type rongxingError struct {
|
||
Code string
|
||
Message string
|
||
}
|
||
|
||
func (e *rongxingError) Error() string {
|
||
return fmt.Sprintf("戎行返回错误,code: %s,message: %s", e.Code, e.Message)
|
||
}
|
||
|
||
// NewRongxingError 创建戎行业务错误
|
||
func NewRongxingError(code, message string) *rongxingError {
|
||
if message == "" {
|
||
if desc := GetCodeMessage(code); desc != "" {
|
||
message = desc
|
||
} else {
|
||
message = "戎行返回未知错误"
|
||
}
|
||
}
|
||
return &rongxingError{Code: code, Message: message}
|
||
}
|
||
|
||
// MapNonBillableToErr 不扣费场景下,将返回码映射为内部哨兵错误
|
||
func MapNonBillableToErr(code string) error {
|
||
switch code {
|
||
case CodeSuccess, CodeNoData:
|
||
return ErrNotFound
|
||
default:
|
||
return ErrDatasource
|
||
}
|
||
}
|