Why is my custom error handler not caching throws inside setTimeout inside runOutsideAngular

Discussão em 'Angular' iniciado por DerZyklop, Abril 9, 2025.

  1. DerZyklop

    DerZyklop Guest

    I have an Angular App with a custom error handler which works fine.

    Errors like these are caught:

    throw new Error('This error gets passed to the custom error handler');

    this.zone.runOutsideAngular(() => {
    throw new Error('This error gets passed to the custom error handler');
    });

    window.setTimeout(() => {
    throw new Error('This error gets passed to the custom error handler');
    }, 100);


    But throws never reach the custom error handler when a setTimeout(...) is placed inside a NgZone's zone.runOutsideAngular(...), like these:

    this.zone.runOutsideAngular(() => {
    window.setTimeout(() => {
    throw new Error('Does not trigger custom error handler');
    }, 100);
    });


    Not even if I let the throw run inside the zone like this:

    this.zone.runOutsideAngular(() => {
    window.setTimeout(() => {
    this.zone.run(() => {
    throw new Error('Does not trigger custom error handler');
    });
    }, 100);
    });


    Why is that the case, and how can catch errors inside setTimeout inside runOutsideAngular?

    Minimal reproduction: https://stackblitz.com/edit/angular-custom-error-handler-cotevkm9

    Continue reading...

Compartilhe esta Página