2

I need to trap all of the listed PurchasesErrorCode error codes in my Flutter app so I can respond to them accordingly.

Currently I can only trap "userCancelled", for everything else I can only report the information returned in the standard PlatformException code, message and details properties, without knowing what they will contain.

try {

  // Code to make purchase..

} on PlatformException catch (e) {

  if (!(e.details as Map)["userCancelled"]) {

    // Here I need a comprehensive switch statement so I can
    // retry where appropriate/control what messages the user sees

    String reason = '';
    (e.details as Map).forEach((k,v) => reason += '$k => $v');
    showError(context, 'Error', '${e.code} : ${e.message}');

  } else {

    showError(context, 'Purchase Cancelled', 'Your purchase was not completed, you have not been charged.');

  }
}

These codes are exposed in IOS/Swift and Android/Kotlin but I can't get them in Flutter/Dart - what am I missing?

liondog
  • 35
  • 6
  • may be other excepts are not PlatformException ? use try and catch and see – Praneeth Dhanushka Fernando Oct 16 '19 at 10:00
  • Thanks, but the RevenueCat documentation says that all exceptions will be thrown as PlatformException. And I can't use try..catch to examine all possible RevenueCat exceptions since I can't trigger them. – liondog Oct 16 '19 at 10:15
  • It seems to me that what you really want to know is what are the error codes held in RevenueCat's `PlatformException`s. Am I right? – Hugo Passos Oct 16 '19 at 12:56
  • Yes, but exposed as enums or equivalent in Dart so I don't have to hard-code them in and risk breaking in the future. As a temporary solution I could build my own enums (or even parse the description), but this isn't ideal. The APIs for other languages expose them... – liondog Oct 16 '19 at 13:12

2 Answers2

3

I developed the RevenueCat Flutter plugin and I created an issue on GitHub some time ago to track this (https://github.com/RevenueCat/purchases-flutter/issues/3). I am sorry there is some room for improvement in our Flutter error handling.

When we send the platform exceptions we pass the error code as an String:

result.error(error.getCode().ordinal() + "", error.getMessage(), userInfoMap);

Too bad we can't just pass an int as the first parameter and we have to pass a String, I guess we could pass it in the userInfoMap. But for now, since we are not providing the enum with the error codes yet, you would have to do something like this in your code:

enum PurchasesErrorCode {
  UnknownError,
  PurchaseCancelledError,
  StoreProblemError,
  PurchaseNotAllowedError,
  PurchaseInvalidError,
  ProductNotAvailableForPurchaseError,
  ProductAlreadyPurchasedError,
  ReceiptAlreadyInUseError,
  InvalidReceiptError,
  MissingReceiptFileError,
  NetworkError,
  InvalidCredentialsError,
  UnexpectedBackendResponseError,
  ReceiptInUseByOtherSubscriberError,
  InvalidAppUserIdError,
  OperationAlreadyInProgressError,
  UnknownBackendError,
  InsufficientPermissionsError
}

try {
} on PlatformException catch (e) {
  PurchasesErrorCode errorCode = PurchasesErrorCode.values[int.parse(e.code)];
  switch (errorCode) {
    case PurchasesErrorCode.UnknownError:
    case PurchasesErrorCode.PurchaseCancelledError:
    case PurchasesErrorCode.StoreProblemError:
    // Add rest of cases
  }
}

When you do e.details, you also get access to a readableErrorCode containing the name of the error code; and an underlyingErrorMessage, that can hopefully help you debug any issue.

I hope that helps

Cesar
  • 682
  • 3
  • 15
1

The issue mentioned on the previous answer was solved, after version 1.0.0 you can handle like this:

try {
    PurchaserInfo purchaserInfo = await Purchases.purchasePackage(package);
} on PlatformException catch (e) {
    var errorCode = PurchasesErrorHelper.getErrorCode(e);

    if (errorCode == PurchasesErrorCode.purchaseCancelledError) {
      print("User cancelled");
    } else if (errorCode == PurchasesErrorCode.purchaseNotAllowedError) {
      print("User not allowed to purchase");
    }
}
Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73