Skip to content

Commit

Permalink
New custom error handler to handle all the unmanaged application errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nicorac committed Dec 6, 2024
1 parent 326861d commit 4079033
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/app/utils/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ErrorHandler, Injectable } from '@angular/core';
import { MessageBoxService } from '../services/message-box.service';

@Injectable()
export class CustomErrorHandler implements ErrorHandler {

private lastMessage = '';
private lastStack = '';

constructor(
private mbs: MessageBoxService,
) {
window.addEventListener('unhandledrejection', (event) => {
this.handleError(event.reason);
});
}

handleError(error: any) {

// extract error data
let message = error?.message ? error.message : error.toString();
let stack = error?.stack ? error.stack : '';

// avoid repeated errors (i.e. template errors in a @for loop)
if (this.lastMessage === message && this.lastStack === stack) {
return;
}
this.lastMessage = message;
this.lastStack = stack;

// error is logged to console, as usual
console.error(error);

this.mbs.showError({
error: message,
message: stack,
onConfirm: () => {
// reset last error
this.lastMessage = '';
this.lastStack = '';
}
});
}
}
16 changes: 15 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { provideHttpClient } from '@angular/common/http';
import { APP_INITIALIZER, enableProdMode, importProvidersFrom } from '@angular/core';
import { APP_INITIALIZER, enableProdMode, ErrorHandler, importProvidersFrom } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter, RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy, Platform } from '@ionic/angular';
import { routes } from './app/app-routing.module';
import { AppComponent } from './app/app.component';
import { I18nService } from './app/services/i18n.service';
import { SettingsService } from './app/services/settings.service';
import { CustomErrorHandler } from './app/utils/errorHandler';
import version from './app/version';
import { environment } from './environments/environment';

Expand All @@ -21,6 +22,10 @@ bootstrapApplication(AppComponent, {
importProvidersFrom(
IonicModule.forRoot({ innerHTMLTemplatesEnabled: true }),
),
{
provide: ErrorHandler,
useClass: CustomErrorHandler,
},
{
provide: RouteReuseStrategy,
useClass: IonicRouteStrategy,
Expand Down Expand Up @@ -61,5 +66,14 @@ function initializeApp(i18n: I18nService, settings: SettingsService, platform: P
await i18n.initialize();
await i18n.load(settings.culture ? settings.culture : settings.defaultCulture);

// intercept unmanaged errors
window.onerror = function (message, file, line, col, error) {
alert("Error occurred: " + error?.message);
return false;
};
window.addEventListener('unhandledrejection', function (e) {
alert("Error occurred: " + e.reason.message);
})

}
}

0 comments on commit 4079033

Please sign in to comment.