Skip to content

Instantly share code, notes, and snippets.

@lydemann
Last active June 12, 2020 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lydemann/ab7cfba68d158ce9b222dacaa181ee4d to your computer and use it in GitHub Desktop.
Save lydemann/ab7cfba68d158ce9b222dacaa181ee4d to your computer and use it in GitHub Desktop.
import { ErrorHandler, Injectable, Injector } from '@angular/core';
import { LogService } from '@app/core/log/log.service';
@Injectable()
export class GlobalErrorHandler extends ErrorHandler {
private sentencesForWarningLogging: string[] = [];
constructor(private injector: Injector) {
super();
}
public handleError(error) {
const logService: LogService = this.injector.get(LogService);
const message = error.message ? error.message : error.toString();
if (error.status) {
error = new Error(message);
}
const errorTraceStr = `Error message:\n${message}.\nStack trace: ${error.stack}`;
const isWarning = this.isWarning(errorTraceStr);
if (isWarning) {
logService.logWarning(errorTraceStr);
} else {
logService.logError(errorTraceStr);
}
throw error;
}
private isWarning(errorTraceStr: string) {
let isWarning = true;
// Error comes from app
if (errorTraceStr.includes('/src/app/')) {
isWarning = false;
}
this.sentencesForWarningLogging.forEach((whiteListSentence) => {
if (errorTraceStr.includes(whiteListSentence)) {
isWarning = true;
}
});
return isWarning;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment