react-native-purchases
react-native-purchases copied to clipboard
Vend a typeguard function for checking if an Error is a PurchasesError
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.
I don't see this being controversial, so I think I'll go ahead and just make the change and submit a PR soon.
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 🙌
@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 😄
Does this do the job?
import { PurchasesError } from 'react-native-purchases';
const isRevenueCatPurchaseError = (error: any): error is PurchasesError => {
return error.domain === 'RevenueCat.ErrorCode';
};