Skip to content

Commit

Permalink
feat: add global error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnygerard committed Dec 18, 2024
1 parent 52fb151 commit 9eb89d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -11,5 +16,9 @@ export const appConfig: ApplicationConfig = {
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideClientHydration(withEventReplay()),
{
provide: ErrorHandler,
useClass: GlobalErrorHandler,
},
],
};
11 changes: 11 additions & 0 deletions src/app/global-error-handler.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
}

0 comments on commit 9eb89d9

Please sign in to comment.