Home > database >  How to handle simple error in Kotlin function Android
How to handle simple error in Kotlin function Android

Time:01-26

I'm having trouble handling error in the following function. I'm basically new to Kotlin. Here's my RevenueCat Login Code and I want to handle ::error in this code:

Purchases.sharedInstance.logInWith(
                    myUserID,
                    ::error // <- How to handle this? I want to retrieve error Code and Error Message.
            )
{ customerInfo, created  ->

// Handle Successful login here

}
          

Here's the code behind the function (within RevenueCat SDK)

@Suppress("unused")
fun Purchases.logInWith(
    appUserID: String,
    one rror: (error: PurchasesError) -> Unit = ON_ERROR_STUB,
    onSuccess: (customerInfo: CustomerInfo, created: Boolean) -> Unit
) {
    logIn(appUserID, logInSuccessListener(onSuccess, one rror))
}

CodePudding user response:

The double colon in ::error is a function reference. It is basically a reference to the function error().

And from your logInWith() function, we have onError: (error: PurchasesError) -> Unit = ON_ERROR_STUB, meaning that the function should take PurchasesError as input parameter and does not need to return.

So we can derive a function as the following:

fun error(error: PurchasesError) {
    // And you can do something with the error here
}

CodePudding user response:

I solved it like this:

Purchases.sharedInstance.logInWith(
     myUserID, 
 one rror = { error -> 
        
        // Handle error here 
        
        }
  • Related