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

sample log v2 :)
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With