sdk_tb_linkedmall-202608271.../sdk_tb_linkedmall/errors.go

58 lines
1.8 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"
)