react-native-purchases icon indicating copy to clipboard operation
react-native-purchases copied to clipboard

Vend a typeguard function for checking if an Error is a PurchasesError

Open bilalq opened this issue 3 years ago • 4 comments

Hey. When using TypeScript, the type of the caught error is unknown (any in non-strict mode of older versions of TypeScript). It would be very useful to have a typeguard function vended that could narrow the type. Would you be willing to accept a PR that implements this?

export const isPurchasesError = (err: unknown): err is PurchasesError => {
  // Implementation here
}

The usage would look like:

try {
  await someCodeThatTriggersAPurchase()
} catch (err) {
  if (isPurchasesError(err)) {
    // Handle PurchasesError here. 
    // The type will be narrowed by the compiler and IDE completion will work nicely.
  } else {
    // Handle other errors differently
  }
}

This is something we can of course just write on our own end, but it seems like it'd be useful to have as part of the core library here.

bilalq avatar Sep 20 '21 23:09 bilalq

I don't see this being controversial, so I think I'll go ahead and just make the change and submit a PR soon.

bilalq avatar Sep 29 '21 14:09 bilalq

I just realized that we forgot to reply here!!! I'm sorry about that @bilalq 🤦

This is a great idea! Thanks for opening up the ticket.

We're more than happy to review the PR 🙌

aboedo avatar Sep 29 '21 17:09 aboedo

@bilalq did you happen to make any progress with this? We're going through all our issues now to make sure we keep things fresh 😄

taquitos avatar Nov 18 '21 18:11 taquitos

Does this do the job?

import { PurchasesError } from 'react-native-purchases';

const isRevenueCatPurchaseError = (error: any): error is PurchasesError => {
  return error.domain === 'RevenueCat.ErrorCode';
};

djw27 avatar Feb 24 '23 11:02 djw27