添加文件: sdk_tb_linkedmall/errors.go
This commit is contained in:
parent
8ab089203d
commit
4ed4d6131f
|
|
@ -0,0 +1,58 @@
|
||||||
|
package sdk_tb_linkedmall
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// APIError 表示API调用返回的业务错误
|
||||||
|
type APIError struct {
|
||||||
|
Code string // 错误码
|
||||||
|
Message string // 错误信息
|
||||||
|
SubCode string // 子错误码
|
||||||
|
SubMessage string // 子错误信息
|
||||||
|
RequestId string // 请求ID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *APIError) Error() string {
|
||||||
|
return fmt.Sprintf("linkedmall api error: code=%s, message=%s, subCode=%s, subMessage=%s, requestId=%s",
|
||||||
|
e.Code, e.Message, e.SubCode, e.SubMessage, e.RequestId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAPIError 创建API错误
|
||||||
|
func NewAPIError(code, message, subCode, subMessage, requestId string) *APIError {
|
||||||
|
return &APIError{
|
||||||
|
Code: code,
|
||||||
|
Message: message,
|
||||||
|
SubCode: subCode,
|
||||||
|
SubMessage: subMessage,
|
||||||
|
RequestId: requestId,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsBusinessSuccess 判断业务是否成功(需要HTTP 200 + Success=true)
|
||||||
|
func IsBusinessSuccess(resp *CommonResponse) bool {
|
||||||
|
return resp != nil && resp.Success
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckResponse 检查响应是否成功,失败返回error
|
||||||
|
func CheckResponse(resp *CommonResponse) error {
|
||||||
|
if resp == nil {
|
||||||
|
return fmt.Errorf("linkedmall: response is nil")
|
||||||
|
}
|
||||||
|
if !resp.Success {
|
||||||
|
return NewAPIError(resp.Code, resp.Message, resp.SubCode, resp.SubMessage, resp.RequestId)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 预定义错误码常量
|
||||||
|
const (
|
||||||
|
ErrCodeShopTypeInvalid = "ShopTypeInvalid"
|
||||||
|
ErrCodeSkuPriceNotUnique = "SkuPriceUnique"
|
||||||
|
ErrCodePurchaseOrderNotFound = "PurchaseOrderNotFound"
|
||||||
|
ErrCodeRefundNumberMustLessThanOrder = "RefundNumberMustLessThanOrder"
|
||||||
|
ErrCodeRefundAmountMustLessThanOrder = "RefundAmountMustLessThanOrder"
|
||||||
|
ErrCodeHasNoPrivilege = "HasNoPrivilege"
|
||||||
|
ErrCodeOrderNotFound = "OrderNotFound"
|
||||||
|
ErrCodeShopNotFind = "ShopNotFind"
|
||||||
|
ErrCodeSavePurchaseOrderError = "SavePurchaseOrderError"
|
||||||
|
ErrCodeOuterPurchaseOrderIdExist = "OuterPurchaseOrderIdExist"
|
||||||
|
)
|
||||||
Loading…
Reference in New Issue