Managing an exception for the second time

In my project, I recently extended the ExceptionHandler class in Angular:

import {Injectable, ExceptionHandler} from '@angular/core';

export class FirstError extends Error {
    handle() {
        console.log("This is firstError handler");
    }
}

export class AnotherError extends Error {
    handle() {
        console.log("This is AnotherError handler");
    }
}

export class _ArrayLogger {
    res = [];
    log(s: any): void { this.res.push(s); }
    logError(s: any): void { this.res.push(s); }
    logGroup(s: any): void { this.res.push(s); }
    logGroupEnd() {};
}

@Injectable()
export class CustomExceptionHandler extends ExceptionHandler {
    constructor() {
        super (new _ArrayLogger(), true);
    }

    call(exception: any, stackTrace = null, reason = null) {
        // My intention is to keep original handler's behavior of logging exceptions to console
        super.call(exception, stackTrace, reason);

        // In addition to that, if the exception belongs to a specific type, I want to perform custom actions
        if (exception.originalException instanceof Error) {
            exception.originalException.handle();
        }
    }
}

The main objective behind this customization is to ensure that the original Angular2 ExceptionHandler logs errors to the console as usual, while also implementing a custom handling for certain types of exceptions. However, I have encountered an issue where the super method is only called the first time an error occurs, and subsequent errors do not trigger the super method. How can I modify this setup so that both the default console logging and custom processing are consistently applied to all errors?

Answer №1

Encountering a similar issue, I managed to resolve it by including false as the second parameter (rethrowException) in the constructor.

constructor() {
    super(new _ArrayLogger(), false);
}

It may seem counterintuitive that the parameter name suggests using true, but in this case, setting it to false did the trick.

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 I customize the appearance of the file input button in Angular Material?

Despite browsing numerous similar questions, I have yet to find a solution to my issue. My setup includes Angular 11, Angular Material 8, and a file input structured like this: <div class="form-group"> <input #myInput type="fil ...

Coverage testing is not embracing all aspects

Currently, I am tackling an Angular 2 project and in the process of writing test cases for the services. It's odd that previously everything was working flawlessly, but now I'm encountering some "no provider" errors such as (No provider for AppSe ...

Ensure that only numerical values in decimal form are permitted when entering data in Angular

How can I restrict user input to only decimal values? Here is the HTML code for my input field: <label for="conversion-factor">Conversion Factor:</label> <input type="text" class="form-control form-control-sm" id="conversion-factor" ...

Angular Typescript consistently returns false when comparing arrays

I've been working on comparing two nested arrays, but it keeps returning false. The goal is to compare the saleLineList in each saleBlock from this.saleBlockList with the saleLineList in each saleBlock from saleBlockList. Even after modifying the da ...

Typescript throwing error TS2307 when attempting to deploy a NodeJS app on Heroku platform

Encountering an error when running the command git push heroku master? The build step flags an error, even though locally, using identical NodeJS and NPM versions, no such issue arises. All automated tests pass successfully without any errors. How can this ...

Adjust CSS style height dynamically

I am currently creating a leaderboard for users: <div class="user"> <ul id="jogadores" *ngFor="let u of sortedUsers$; let i=index;" (click)="routeToUser(u.id)"> <li class="user"> <img class="gravatar" src="https://www.gra ...

Starting the process of configuring Angular 5 with Express using TypeScript

Hi there! I am looking to create a fresh application using angular 5 and express (typescript for express as well). Do you have any helpful guides or tips that could assist me in reaching my objective? Appreciate all the help, Giuseppe ...

Guide on including a viewmodel object in cookies using AngularJS 2

Recently, I've been attempting to store viewmodel objects in cookies using AngularJS2. Despite my efforts, I haven't managed to make it work yet. private setCookie(name: string, value: any, expireDays: number, path: string = "") { let d: Dat ...

Tips for creating a carousel with Angular 9 to showcase numerous items

I've got this code snippet that I'm working on. I want to incorporate a Carousel feature using Angular 9 without relying on any external libraries. Currently, all the data items are appearing in a single row (they are exceeding the specified bor ...

Encountering a Next.js event type issue within an arrow function

After creating my handleChange() function to handle events from my input, I encountered an error that I'm unsure how to resolve. Shown below is a screenshot of the issue: https://i.sstatic.net/fWJA2.png I am currently working with Next.js. In React ...

Using Angular services without specifying a specific type

Currently, I have a component that needs to make an API request. In addition, there exists an abstract class designed for handling such services with the following constructor: export class ODataService<T> extends ApiService { constructor(http: Htt ...

What is the best way to search and sort a MatTable with special characters like accents and diacrit

I need to implement a name filtering functionality for a table, regardless of whether the user includes accents or not. For instance: If the user types "hydrogen", the result should display "Hydrôgen" from the table. I am using Angular 8.1.3 and Angula ...

Troubleshooting: Vue 3 Vite encountering 404 error when attempting to load fonts from assets using Font Loading API

Attempting to dynamically load fonts from the assets directory within Vue 3 (Typescript) using Vite has led to a 404 error occurring. https://i.sstatic.net/H3Ho7.png const fonts = import.meta.glob('@/assets/fonts/*.otf') console.log(fonts) asy ...

What is the correct way to fetch all attributes of a particular key type from an interface using TypeScript?

Exploring arrays, I discovered the following setup: const MyArray = [ { name: "Alice", age: 15 }, { name: "Bob", age: 23 }, { name: "Eve", age: 38 }, ]; type Person = typeof MyArray[number]; Curious, I attempted a sim ...

Steps for setting up a project to compile for ES6 syntax:

Working on a project using Angular 2 + PrimeNG, I encountered an issue with TypeScript compilation while trying to use Git. The solution involved adjusting the package.json file. "dependencies": { "@angular/common": "2.4.2", // List of dependencies goes ...

What is the proper way to define the type of an object when passing it as an argument to a function in React using TypeScript?

I'm struggling to figure out the appropriate type definition for an Object when passing it as an argument to a function in React Typescript. I tried setting the parameter type to "any" in the function, but I want to avoid using "any" whenever passing ...

Angular Material Rotate Ink Bar to Vertical Orientation

After wanting to change the orientation of Angular Material's Tab Component to vertical, I visited this page (Tabs) and experimented with the CSS. By making the necessary adjustments, I successfully displayed the tabs vertically using the following CS ...

Changing Image to Different File Type Using Angular

In my Angular Typescript project, I am currently working on modifying a method that uploads an image using the input element of type file. However, I no longer have an input element and instead have the image file stored in the assets folder of the project ...

Lazy loading in Angular 14 delays the loading of a page until the content is clicked, ensuring a smoother user experience with reduced loading times

Greetings, I've been stuck on this issue for quite some time now. The problem I'm facing is that the page doesn't load until I click on the website. Can anyone please assist me with this? My expectation is that the default setting I have co ...

A guide on retrieving the index of an element within a nested array

Is it possible to determine the index of an item within a nested array without knowing how deeply nested it is? For example, given the array arr=[a,[b,[c],[d],e],f,[g],h]. ...