-
-
Save lydemann/ab7cfba68d158ce9b222dacaa181ee4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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