Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/angular/ssr/src/utils/ng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ function asyncDestroyPlatform(platformRef: PlatformRef): Promise<void> {
if (!platformRef.destroyed) {
platformRef.destroy();
}
} catch (error) {
// An error thrown by a destroy hook must not escape this macrotask,
// otherwise it becomes an uncaught exception that can terminate the process.
// eslint-disable-next-line no-console
console.error('An error occurred while destroying the platform.', error);
} finally {
resolve();
}
Expand Down
40 changes: 40 additions & 0 deletions packages/angular/ssr/test/utils/ng_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import '@angular/compiler';
import {
ApplicationRef,
Component,
OnDestroy,
PlatformRef,
inject,
provideZonelessChangeDetection,
Expand Down Expand Up @@ -280,4 +281,43 @@ describe('renderAngular', () => {
await setTimeout(0);
expect(destroySpy).toHaveBeenCalledTimes(1);
});

it('should log errors thrown by destroy hooks instead of leaving them uncaught', async () => {
const error = new Error('Destroy hook error');
const consoleErrorSpy = spyOn(console, 'error');

@Component({
selector: 'app-root',
template: 'Destroy hook error works',
})
class AppComponent implements OnDestroy {
ngOnDestroy(): void {
throw error;
}
}

const result = await renderAngular(
BASE_HTML,
(context) =>
bootstrapApplication(
AppComponent,
{ providers: [provideZonelessChangeDetection()] },
context,
),
new URL('http://localhost/'),
[],
'ssr',
);

expect(result).toEqual(jasmine.objectContaining({ destroy: jasmine.any(Function) }));
if (!('destroy' in result)) {
return;
}

await expectAsync(result.destroy()).toBeResolved();
expect(consoleErrorSpy).toHaveBeenCalledOnceWith(
'An error occurred while destroying the platform.',
error,
);
});
});