Alwaysblue
Alwaysblue

Reputation: 11830

unable to catch errors in react-native

How Can I handle error in React-Native

So this is my error enums

export const frontendErrors = {
    INVALID_PHONE_NUMBER_ENTERED: 'Sorry you have entered invalid phone number'
}

From my phoneAuth.js file, I am throwing error in catch statement like this

catch (err) {
    const errorSearch = 'auth/invalid-phone-number'
    if (err.message.includes(errorSearch)) {
     throw new Error(frontendErrors.INVALID_PHONE_NUMBER_ENTERED)
    }

And When I catching the error in the parent component

catch (error) {
           console.log(error)
            this.dropDownAlertRef.alertWithType('error', 'Error', error)
        }

in the log I am getting my error as an object and like this

Error: Sorry you have entered invalid phone number

The dropdownAlertRef takes in string so it is logging an empty object i.e.

this.dropDownAlertRef.alertWithType('error', 'Error', 'Invalid phone number')

this will log error.

What have I tried?

Doing, console.log(error.Error) gives undefined. How can I access my error?

Upvotes: 2

Views: 1100

Answers (1)

Kamal Pandey
Kamal Pandey

Reputation: 1598

You can do something like this.

console.log(error.message)

For more refrence you can refer here

Upvotes: 2

Related Questions