Skip to content

Instantly share code, notes, and snippets.

@CaselIT
Last active August 4, 2016 15:34
Show Gist options
  • Save CaselIT/a022698a10a9a3b6c234c2eb5a73c663 to your computer and use it in GitHub Desktop.
Save CaselIT/a022698a10a9a3b6c234c2eb5a73c663 to your computer and use it in GitHub Desktop.
Angular custom exception handler
/**
* Custom logger that saves the logging.
* The console logging logic if from BrowserDomAdapter
* https://github.com/angular/angular/blob/master/modules/@angular/platform-browser/src/browser/browser_adapter.ts#L69
*/
class CustomLogger {
errorLog: any[] = [];
logError(error: any) {
this.errorLog.push(error);
if (window.console.error) {
window.console.error(error);
} else {
window.console.log(error);
}
}
log(error: any) {
this.errorLog.push(error);
window.console.log(error);
}
logGroup(error: any) {
this.errorLog.push(error);
if (window.console.group) {
window.console.group(error);
this.logError(error);
} else {
window.console.log(error);
}
}
logGroupEnd() {
if (window.console.groupEnd) {
window.console.groupEnd();
}
}
}
/**
* Custom exception handler.
*/
export class CustomExceptionHandler extends ExceptionHandler {
private logger: CustomLogger;
constructor() {
const logger = new CustomLogger();
super(logger, true);
this.logger = logger;
}
call(error: any, stackTrace: any = null, reason: string = null) {
try {
super.call(error, stackTrace, reason);
} catch (exception) {
throw exception;
} finally {
// do something with the exception in this.logger.errorLog
// Clear the logger array
this.logger.errorLog.length = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment