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."); + } +}