Issue
Using a custom error handler, is there any chance to tell runtime template errors and usual errors apart? If you e.g. put something like {{x.y}}
in the template, with x: any;
you'll get an error
TypeError: Cannot read properties of undefined (reading 'y')
Now this error sure comes again and again with every app cycle. Is there any chance to detect these in an ErrorHandler?
export class ErrorService implements ErrorHandler {
handleError(error: any): void {
/*
Sure this equality doesn't work. But how to tell:
a) If the error is caused by an Angular Template
b) If it is the same template error that caused the last call
*/
if (error == "templateError") {
if (error == lastTemplateError) { return; }
lastTemplateError = error;
}
//Handle my error here; using tick to get a recursion, if double error isn't catched
this.injector.get(ApplicationRef).tick();
}
injector = inject(Injector);
lastTemplateError: any;
}
EDIT: To me it seems like this currently isn't possible. I've opened a Git feature request. Feel free to promote.
Solution
Theoretically it can be done by checking for presense within stack
of some keyword from template execution(e.g. at executeTemplate
)
let lastTemplateError: any;
export class ErrorService extends ErrorHandler {
override handleError(error: any): void {
const isTemplateError = error.stack?.includes('at executeTemplate');
if (lastTemplateError && isTemplateError && error.stack == lastTemplateError.stack) {
return;
}
if (isTemplateError) {
lastTemplateError = error;
}
return super.handleError(error);
}
}
Answered By - yurzui
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.