Full answer
There are two kinds of failures. Expected failures, such as a 404 from an API or a validation error, should be handled where they happen, with a message in the UI. Unexpected failures, such as a bug that throws in a template or a rejected promise nobody caught, need a global safety net.
ErrorHandler is the global hook. Angular calls handleError(error) for errors thrown in templates, event handlers, lifecycle hooks, and in effects. The default writes to the console. To replace it:
{ provide: ErrorHandler, useClass: GlobalErrorHandler }
In handleError you typically:
- Normalise the error (some are wrapped, some are strings).
- Send it to monitoring (Sentry, or your own endpoint) with context such as the route and app version. Do not send personal data, tokens, or form values.
- Show a short, friendly message through a notification service, without technical details.
- Avoid loops: if reporting fails, do not throw from the handler.
Which errors reach it: by default only errors Angular catches. provideBrowserGlobalErrorListeners() (added in Angular 20 and included in new project templates) registers error and unhandledrejection listeners on window and forwards those to ErrorHandler. This matters in zoneless apps, which have no zone to catch them.
HTTP errors: do not treat all HttpErrorResponse as crashes. An interceptor can map status codes to messages (401 goes to login, 403 shows "not allowed", 5xx shows "try again") and rethrow so the calling code can still react. Reserve ErrorHandler for the unexpected.
UI recovery: Angular has no built-in error boundary component. Keep errors local with catchError and a fallback state in the part of the page that failed, and offer a retry action.
// file: notifier.service.ts
import { Injectable, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class Notifier {
readonly message = signal<string | null>(null);
show(text: string): void {
this.message.set(text);
}
clear(): void {
this.message.set(null);
}
}
// file: global-error-handler.ts
import { HttpErrorResponse } from '@angular/common/http';
import { ErrorHandler, Injectable, inject } from '@angular/core';
import { Router } from '@angular/router';
import { Notifier } from './notifier.service';
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
private readonly notifier = inject(Notifier);
private readonly router = inject(Router);
private readonly seen = new Set<string>();
handleError(error: unknown): void {
// HTTP errors are expected and are handled by interceptors and services.
if (error instanceof HttpErrorResponse) {
return;
}
const message = error instanceof Error ? error.message : String(error);
if (this.seen.has(message)) {
return; // do not report the same error many times
}
this.seen.add(message);
try {
// Replace with a monitoring client. Do not send tokens or form values.
console.error('Unexpected error', { message, route: this.router.url });
} catch {
// Never throw from an error handler.
}
this.notifier.show('Something went wrong. Please try again.');
}
}
// file: app.config.ts
import { ApplicationConfig, ErrorHandler, provideBrowserGlobalErrorListeners } from '@angular/core';
import { provideRouter } from '@angular/router';
import { GlobalErrorHandler } from './global-error-handler';
export const appConfig: ApplicationConfig = {
providers: [
provideBrowserGlobalErrorListeners(),
provideRouter([]),
{ provide: ErrorHandler, useClass: GlobalErrorHandler },
],
};
// file: banner.component.ts
import { Component, inject } from '@angular/core';
import { Notifier } from './notifier.service';
@Component({
selector: 'app-error-banner',
template: `
@if (notifier.message(); as text) {
<div role="alert">{{ text }} <button (click)="notifier.clear()">Dismiss</button></div>
}
`,
})
export class ErrorBannerComponent {
protected readonly notifier = inject(Notifier);
}Angular 20.3, strict template check passes, and a Karma test calls the handler. The reporter only logs to the console here. Replace it with a real monitoring client.
Key points
- `ErrorHandler.handleError` is the global hook. Replace it with `{ provide: ErrorHandler, useClass: ... }`.
- `provideBrowserGlobalErrorListeners()` forwards window errors and unhandled rejections to it.
- Handle expected HTTP errors in interceptors and services, not in the global handler.
- Log with context but without personal data. Show friendly messages.
Common mistakes
- Sending form values or tokens to the monitoring service.
- Throwing an error inside the error handler.
- Showing raw error messages and stack traces to users.
Follow-ups
- How would you avoid reporting the same error a thousand times?
- How do you handle errors in a zoneless application?
- How would you keep one failing widget from breaking the page?
Tips
- Separate expected from unexpected errors. This is the key idea.
- Mention privacy of logged data.
- Say that you add local fallbacks, since there is no error boundary.
Evaluation rubric
- Replaces ErrorHandler correctly.
- Knows what reaches it and the Angular 20 listener provider.
- Handles HTTP errors separately and considers privacy.