Properly managing exceptions in your Ionic 2 application

I have developed an ionic2 app that utilizes firebase authentication. I am currently facing an issue when handling the "auth/account-exists-with-different-credential" error, specifically when a user tries to log in with the same email using multiple providers (such as Google and Facebook).

constructor(public af: AngularFire,public navCtrl: NavController) {
    firebase.auth().getRedirectResult()
      .then(res=>{
        console.log('success',res)
      })
  .catch((err:any)=>{
    console.log(error.message)
    if (err.code=='auth/account-exists-with-different-credential'){
       doaccountlink();
    }
  })
  }



    fblogin(){
        firebase.auth().signInWithRedirect(new firebase.auth.FacebookAuthProvider())
      }

      gglogin(){
        firebase.auth().signInWithRedirect(new firebase.auth.GoogleAuthProvider())
      }

Although I am able to catch the error code in the catch block as expected, the problem arises when the exception continues to display in the console and on my page (as shown in the attached image). I am relatively new to JavaScript/TypeScript. In Java, if an error is already caught in the catch block, it should not continue to be displayed. Is there something wrong with my code? How can I prevent this exception from showing on my page?

https://i.sstatic.net/r79o8.png

Answer №1

To gain a better understanding of the issue, it would be necessary to thoroughly debug your code as there may be another exception occurring elsewhere that is causing the displayed page. However, here are some useful tips:

  1. The page shown with the exception is due to the presence of the IonicErrorHandler. You can locate this in your app.module.ts file within the providers array, where you will find the following line:

    providers: [
        // services...
        { provide: ErrorHandler, useClass: IonicErrorHandler }
    ]
    

    This essentially means that Ionic is extending Angular 2's default ErrorHandler with its own class. It's worth noting that this extension only occurs during development and not in production (more information can be found in the source code here).

  2. If it suits the context of your app, you have the option to create your own error handler and manage exceptions using custom code. As demonstrated here, you can achieve this by implementing a new class that conforms to the ErrorHandler interface:

    class MyErrorHandler implements ErrorHandler {
        handleError(err: any): void {
            // handle the error appropriately
        }
    }
    

    You can then replace the

    { provide: ErrorHandler, useClass: IonicErrorHandler }

    with

    { provide: ErrorHandler, useClass: MyErrorHandler }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can we ensure Angular method completes before moving on to the next line of code?

When I try to call the method getConfigSettings() inside a component, it ends up calling another method from a service that returns an observable collection. The problem arises when the lines inside the main method (getConfigSettings()) are not executed i ...

How can a child component in Angular 4 automatically update the value of its parent component without requiring any additional actions from the parent component?

Is it possible to achieve an automatic update (2-way binding) of a property in the parent controller from a child controller without explicitly passing an @Output event into the child controller, similar to the method shown in the following example: Plunk ...

Encountering a SubprocessError while attempting an Ionic v4 production build

Encountering issues with Ionic v4 production builds on Ubuntu v.18. Upon running ionic build --prod, an error is thrown: Error at new SubprocessError (/root/.nvm/versions/node/v12.2.0/lib/node_modules/@ionic/cli/node_modules/@ionic/utils-subprocess/dist/i ...

Creating a declaration file in TypeScript: defining types and interfaces

Could you guide me on creating a declaration file for define(function() { 'use strict'; return Object.freeze({ BTN_LINK: 'btnLink', COMBO_BOX: 'comboBox', TEXT: 'text' }); }); ...

Leveraging LESS in an Angular2 component

Currently, I am attempting to integrate LESS with angular2. To do so, I have imported the less.js file in my index.html and I am using the following command within the component: less.modifyVars({ '@currentTheme-bar': '@quot ...

After updating the TypeScriptOutDir configuration, breakpoints are not being reached

Currently, I am utilizing Visual Studio Professional 2013 Update 3 and have developed a Node console Application with the simple "hello world" log instruction. Setting a breakpoint in this instruction and running the debugger functions smoothly, hitting th ...

Async function is improperly updating the array state by overwriting it completely instead of just updating one item as

I am working on a file upload feature where each uploaded file should have a progress bar that updates as the file gets uploaded. I'm using a state to keep track of selected files and their respective progress: interface IFiles { file: File; c ...

Pass a string with quotation marks to a component input in Angular 6

Need help with passing a string input to a component: @Component({ selector: 'abcComponent' template: ` <div> .... </div>` }) export class AbcComponent { @Input() text:string; } I am trying to send a strin ...

Launching Angular Application on Amazon Web Services S3 or EC2

I'm currently looking into deploying an Angular application on AWS and I've come across two potential methods: Using an S3 Bucket Deploying on an EC2 instance with nginx Which of these options is considered the best approach and why? The appli ...

The status of the Office.js appointment remains updated even after the saveAsync callback is executed

Utilizing the Office JavaScript API for an Outlook add-in, I encountered a issue with some code designed to save an appointment and close its window. Despite saving the appointment through the API, I continue to receive a "Discard changes" confirmation dia ...

Filtering tables with checkboxes using Next.js and TypeScript

I've recently delved into Typescript and encountered a roadblock. While I successfully tackled the issue in JavaScript, transitioning to Typescript has left me feeling lost. My dilemma revolves around fetching data from an API and populating a table u ...

Oops! An issue has occurred: Control with the specified path (formArray) could not be

I encountered an issue with Angular showing the error message: ERROR Error: Cannot find control with path: 'notes -> 1', 'notes -> 0'. Below is the code snippet from component.html: <div formArrayName="notes"> ...

What sets apart HttpClient.post() from creating a new HttpRequest('POST') in Angular?

I've recently started learning angular, and I've noticed that there are two different ways to make a POST request: constructor(private httpClient: HttpClient) { httpClient.post(url, data, options); } constructor(private httpClient: HttpClie ...

How can I use a single route in Angular 5 to direct all paths for an outlet to a single component?

Here is my current setup: const routes: Routes = [ { path: '', component: NavComponent, outlet: 'nav' }, // (1) { path: '**', component: NavComponent, outlet: 'nav' } // (2) ]; The configuration is functioning ...

What is the correct approach for detecting object collisions in Phaser 3?

Hey everyone, I'm facing a problem and could use some assistance. Currently, I am trying to detect when two containers collide in my project. However, the issue is that the collision is being detected before the objects even start moving on screen. It ...

Merging Data from Multiple Forms in an Angular Application

I am currently working on my first Angular project and although I have made significant progress, I have reached a point where I feel I need assistance to complete it successfully. Project Overview: I have a class mod.ts export interface Mod { id : ...

What is the best way to implement the usehook function in a function using react and typescript?

When attempting to utilize the useHook in a function, an error message appears stating that useHook is being used within a function which is neither a React function component nor a custom React hook. I encountered an error when trying to use the useOpen ...

Using Firestore to retrieve data based on a value within an array's field

I am faced with a challenge of performing a query to find a document that includes a specific value within an object array. Let's take a look at the structure of my database: The query I want to execute is as follows: db.collection('identites&a ...

Oops! Looks like we couldn't locate the template for project number 1234512345 and namespace firebase-server. Let's try to find

Encountering an issue while attempting to utilize Firebase Remote Config in a Server Action within a NextJS application: Error: [NOT_FOUND]: Template not found for project number 1234512345 and namespace firebase-server Although my Firebase Remote Confi ...

Implementing TypeScript type declarations for merging core types

Is there a way to perform type declaration merging in TypeScript for built-in types when using imports? I am currently attempting to merge interfaces as per the guidelines outlined in this documentation: https://www.typescriptlang.org/docs/handbook/declar ...