awa / go-iap

go-iap verifies the purchase receipt via AppStore, GooglePlayStore, AmazonAppStore and Huawei HMS.
MIT License
890 stars 248 forks source link

Consider add error type for request apple store api #229

Open bing-h opened 1 year ago

bing-h commented 1 year ago

for example,function GetALLSubscriptionStatuses return (rsp *StatusResponse, err error),I hope to retry when apple server return response code 429, maybe return custom error type with field status.

type AppstoreAPIError struct {
    err        error
    statusCode int
}
richzw commented 1 year ago

The errCode == 4290000 could meet your requirement, please refer to codes https://github.com/awa/go-iap/blob/master/appstore/api/error.go#L43

func newAppStoreAPIError(b []byte, hd http.Header) (*Error, bool) {
    if len(b) == 0 {
        return nil, false
    }
    var rErr appStoreAPIErrorResp
    if err := json.Unmarshal(b, &rErr); err != nil {
        return nil, false
    }
    if rErr.ErrorCode == 0 {
        return nil, false
    }
    if rErr.ErrorCode == 4290000 {
        retryAfter, err := strconv.ParseInt(hd.Get("Retry-After"), 10, 64)
        if err == nil {
            return &Error{errorCode: rErr.ErrorCode, errorMessage: rErr.ErrorMessage, retryAfter: retryAfter}, true
        }
    }