Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get component name which couses an error in angular 5

I created global handler for errors in my angular app:

export class GlobalErrorHandlerComponent implements ErrorHandler {

  constructor(private injector: Injector) { }

  handleError(error: Error) {
    const router = this.injector.get(Router);
    console.log(error.name);
    console.log(router.url);
    console.log(error.message);
    const route = this.injector.get(ActivatedRoute);
    var snapshot = route.snapshot;
    console.log(snapshot.routeConfig.component.name); //unlucky it doesnt work :(
  }
}

I get all data which I needed for statistics, but I also want name of component which couses an error, I tried many things -one which have some sense - unlucky doesnt work. Is any way to get that info?

edit: sample log enter image description here

sample log v2 :)enter image description here

like image 838
Adam Avatar asked Nov 08 '25 09:11

Adam


1 Answers

As far as I know, viewing the stacktrace through error.stack is the only way to view which component (and which line) is throwing the error.

Be wary though, it seems error.stack does not exist on all browsers, so you might want to use something like this to avoid any double errors:

try {
    console.log(error.stack);
} catch {
    console.log("Your browser does not support printing the stackTrace");
    // Nothing here, code contiues without throwing error.
}
like image 131
Woohoojin Avatar answered Nov 10 '25 10:11

Woohoojin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!