Sub topic 11
Handling errors without zones
This chapter assumes you can enable zoneless mode and know what schedules change detection in a zoneless app. You should also know how to provide a service with inject().
What Zone.js did for errors
In a zone-based app, Angular ran your code inside NgZone. Zone.js patched setTimeout, promises and event listeners, so an error thrown in any of them reached the zone. The zone emitted it on NgZone.onError, and Angular passed it to ErrorHandler. You got one reporting path for most uncaught errors without writing any code.
Without Zone.js, that interception is gone. NgZone becomes a no-op, and onError never emits. An error in a timer callback or an unhandled promise rejection now goes to the browser's global handlers instead of Angular.
Which errors Angular still catches
Angular still reports errors from code it runs itself. That includes template event handlers, lifecycle hooks, effects, and the change detection run that zoneless mode schedules. These go to ErrorHandler.handleError().
Errors from your own asynchronous code are different. They reach ErrorHandler only if you route them there.
Forward global errors to ErrorHandler
Register the global listeners with provideBrowserGlobalErrorListeners(). It listens for error and unhandledrejection on window and forwards both to ErrorHandler. Current Angular CLI templates for zoneless apps already include it.
import { bootstrapApplication } from '@angular/platform-browser';
import {
provideBrowserGlobalErrorListeners,
provideZonelessChangeDetection,
} from '@angular/core';
import { App } from './app/app';
bootstrapApplication(App, {
providers: [
provideZonelessChangeDetection(),
provideBrowserGlobalErrorListeners(),
],
});With this in place, an RxJS subscribe() that has no error callback also reaches ErrorHandler. RxJS rethrows such errors asynchronously, and the global listener picks them up.
Customize the handler
Replace ErrorHandler once, and every path above uses your version.
import { ErrorHandler, Injectable } from '@angular/core';
@Injectable()
export class ReportingErrorHandler implements ErrorHandler {
handleError(error: unknown): void {
console.error('Unhandled application error', error);
// Send the error to your logging service here.
}
}import { ErrorHandler } from '@angular/core';
import { ReportingErrorHandler } from './reporting-error-handler';
export const errorProviders = [
{ provide: ErrorHandler, useClass: ReportingErrorHandler },
];Add errorProviders to the providers array in bootstrapApplication.
Report errors you catch yourself
If you catch an error with try/catch and still want central reporting, call the handler yourself.
import { ErrorHandler, Injectable, inject } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class ProfileLoader {
private readonly errorHandler = inject(ErrorHandler);
async load(url: string): Promise<unknown | null> {
try {
const response = await fetch(url);
return await response.json();
} catch (error) {
this.errorHandler.handleError(error);
return null;
}
}
}Errors in tests
Zoneless TestBed rethrows errors that Angular would normally send to ErrorHandler. A failing change detection run therefore fails the test instead of only logging to the console. This is the default and you should keep it. If a test deliberately provokes an error, set rethrowApplicationErrors: false in TestBed.configureTestingModule and assert on a spy for ErrorHandler instead.
Common mistakes
- Subscribing to
NgZone.onError. It no longer fires, so move that logic into a customErrorHandler. - Assuming a swallowed promise rejection is reported. Without the global listeners it is only a browser console message.
- Catching an error and ignoring it. Call
handleError()or rethrow so the error stays visible.
Exercise
A zoneless app throws inside a setTimeout callback, and your custom ErrorHandler never runs. What is missing, and why did the same code work with Zone.js?
Show answer
The app lacks provideBrowserGlobalErrorListeners(). With Zone.js, the patched setTimeout sent the error to NgZone.onError, which fed ErrorHandler. Without zones, the error only reaches the window error event. That listener forwards it to ErrorHandler, but only once it is registered.