File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -267,6 +267,11 @@ function asyncDestroyPlatform(platformRef: PlatformRef): Promise<void> {
267267 if ( ! platformRef . destroyed ) {
268268 platformRef . destroy ( ) ;
269269 }
270+ } catch ( error ) {
271+ // An error thrown by a destroy hook must not escape this macrotask,
272+ // otherwise it becomes an uncaught exception that can terminate the process.
273+ // eslint-disable-next-line no-console
274+ console . error ( 'An error occurred while destroying the platform.' , error ) ;
270275 } finally {
271276 resolve ( ) ;
272277 }
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import '@angular/compiler';
1414import {
1515 ApplicationRef ,
1616 Component ,
17+ OnDestroy ,
1718 PlatformRef ,
1819 inject ,
1920 provideZonelessChangeDetection ,
@@ -280,4 +281,43 @@ describe('renderAngular', () => {
280281 await setTimeout ( 0 ) ;
281282 expect ( destroySpy ) . toHaveBeenCalledTimes ( 1 ) ;
282283 } ) ;
284+
285+ it ( 'should log errors thrown by destroy hooks instead of leaving them uncaught' , async ( ) => {
286+ const error = new Error ( 'Destroy hook error' ) ;
287+ const consoleErrorSpy = spyOn ( console , 'error' ) ;
288+
289+ @Component ( {
290+ selector : 'app-root' ,
291+ template : 'Destroy hook error works' ,
292+ } )
293+ class AppComponent implements OnDestroy {
294+ ngOnDestroy ( ) : void {
295+ throw error ;
296+ }
297+ }
298+
299+ const result = await renderAngular (
300+ BASE_HTML ,
301+ ( context ) =>
302+ bootstrapApplication (
303+ AppComponent ,
304+ { providers : [ provideZonelessChangeDetection ( ) ] } ,
305+ context ,
306+ ) ,
307+ new URL ( 'http://localhost/' ) ,
308+ [ ] ,
309+ 'ssr' ,
310+ ) ;
311+
312+ expect ( result ) . toEqual ( jasmine . objectContaining ( { destroy : jasmine . any ( Function ) } ) ) ;
313+ if ( ! ( 'destroy' in result ) ) {
314+ return ;
315+ }
316+
317+ await expectAsync ( result . destroy ( ) ) . toBeResolved ( ) ;
318+ expect ( consoleErrorSpy ) . toHaveBeenCalledOnceWith (
319+ 'An error occurred while destroying the platform.' ,
320+ error ,
321+ ) ;
322+ } ) ;
283323} ) ;
You can’t perform that action at this time.
0 commit comments