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?