chargebee / chargebee-flutter

MIT License
5 stars 8 forks source link

Difficult error handling #40

Closed ciriousjoker closed 1 year ago

ciriousjoker commented 1 year ago

Android

Example errors

cancelled configure() not called
Bildschirm­foto 2023-01-21 um 16 56 29 Bildschirm­foto 2023-01-21 um 19 47 01

Error code "null" for everything, are you kidding me?

iOS

Example errors

cancelled on a simulator (not chargebee's fault)
Bildschirm­foto 2023-01-21 um 21 11 41 Bildschirm­foto 2023-01-21 um 21 18 00

On Android, details is a dynamic and apparently contains a stringified stacktrace. On iOS, we get a useless message property instead and need to use details to figure out what happened.

The necessary, ugly workaround

We can't display the English, internal(!) error message to the end user and now we seriously need to check if the error message contains specific words to hopefully distinguish them!?

Here's our "solution", but please just provide error codes instead...

try {
  return await Chargebee.purchaseProduct(product, uid);
} on PlatformException catch (e) {
  // On Android, message will be set (details is a stringified stacktrace)
  if (e.message != null) {
    // Android: "User pressed back or canceled a dialog" <- no, not a typo, just USA...
    if (e.message!.contains("canceled")) return PurchaseResult("", "", PurchaseResultExtension.kCancelled);
  }

  // On iOS, details will be set instead.
  if (e.details != null) {
    // iOS: "User cancelled the payment."
    if (e.details.toString().contains("cancelled")) {
      return PurchaseResult("", "", PurchaseResultExtension.kCancelled);
    }
  }

  // List of known error messages at the time of writing:
  // Android:
  // - "User pressed back or canceled a dialog"
  // - "SDK key not available to proceed purchase"
  // iOS:
  // - "User cancelled the payment."
  // - "Purchase is unavailable due to unknown or unexpected reason. Please try again later."

  return PurchaseResult("", "", PurchaseResultExtension.kError);
}

An actual solution

Please provide useful PlatformExceptions with actual error codes that match(!) between Android and iOS.

cb-amutha commented 1 year ago

@ciriousjoker Sure, Working on it.