Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing all RevenueCat PurchasesErrorCode codes in flutter

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?

like image 612
liondog Avatar asked Jan 19 '26 03:01

liondog


1 Answers

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

like image 199
Cesar Avatar answered Jan 21 '26 23:01

Cesar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!