From 9eb89d9d86e73922ccb3f23a04536649673c3e55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20G=C3=A9rard?= Date: Wed, 18 Dec 2024 03:55:15 +0100 Subject: [PATCH] feat: add global error handler --- src/app/app.config.ts | 11 ++++++++++- src/app/global-error-handler.ts | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/app/global-error-handler.ts diff --git a/src/app/app.config.ts b/src/app/app.config.ts index d407e71..accbd89 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -1,5 +1,10 @@ import { routes } from "@/app/app.routes"; -import { ApplicationConfig, provideZoneChangeDetection } from "@angular/core"; +import { GlobalErrorHandler } from "@/app/global-error-handler"; +import { + ApplicationConfig, + ErrorHandler, + provideZoneChangeDetection, +} from "@angular/core"; import { provideClientHydration, withEventReplay, @@ -11,5 +16,9 @@ export const appConfig: ApplicationConfig = { provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), provideClientHydration(withEventReplay()), + { + provide: ErrorHandler, + useClass: GlobalErrorHandler, + }, ], }; diff --git a/src/app/global-error-handler.ts b/src/app/global-error-handler.ts new file mode 100644 index 0000000..65a360f --- /dev/null +++ b/src/app/global-error-handler.ts @@ -0,0 +1,11 @@ +import { ErrorHandler } from "@angular/core"; + +/** + * Handle uncaught client-side errors. + */ +export class GlobalErrorHandler implements ErrorHandler { + handleError(e: unknown): void { + console.error(e); + alert("An unknown error has occurred. Please try again later."); + } +}