From cde83e99bcded6565fad222dbe5ea73c2c64e24a Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Wed, 29 Jan 2025 14:55:44 -0800 Subject: [PATCH] refactor(core): move callAndReportToErrorHandler to bootstrap file (#59793) This function is only used in the bootstrap file and does not need to be in application_ref PR Close #59793 --- .../core/src/application/application_ref.ts | 24 ----------------- .../src/application/create_application.ts | 2 +- packages/core/src/platform/bootstrap.ts | 26 ++++++++++++++++++- packages/core/src/platform/platform_ref.ts | 6 +---- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/packages/core/src/application/application_ref.ts b/packages/core/src/application/application_ref.ts index 45cbc350f30..9a96a578ab3 100644 --- a/packages/core/src/application/application_ref.ts +++ b/packages/core/src/application/application_ref.ts @@ -41,7 +41,6 @@ import {publishDefaultGlobalUtils as _publishDefaultGlobalUtils} from '../render import {requiresRefreshOrTraversal} from '../render3/util/view_utils'; import {ViewRef as InternalViewRef} from '../render3/view_ref'; import {TESTABILITY} from '../testability/testability'; -import {isPromise} from '../util/lang'; import {NgZone} from '../zone/ng_zone'; import {ApplicationInitStatus} from './application_init'; @@ -176,29 +175,6 @@ export interface BootstrapOptions { /** Maximum number of times ApplicationRef will refresh all attached views in a single tick. */ const MAXIMUM_REFRESH_RERUNS = 10; -export function _callAndReportToErrorHandler( - errorHandler: ErrorHandler, - ngZone: NgZone, - callback: () => any, -): any { - try { - const result = callback(); - if (isPromise(result)) { - return result.catch((e: any) => { - ngZone.runOutsideAngular(() => errorHandler.handleError(e)); - // rethrow as the exception handler might not do it - throw e; - }); - } - - return result; - } catch (e) { - ngZone.runOutsideAngular(() => errorHandler.handleError(e)); - // rethrow as the exception handler might not do it - throw e; - } -} - export function optionsReducer(dst: T, objs: T | T[]): T { if (Array.isArray(objs)) { return objs.reduce(optionsReducer, dst); diff --git a/packages/core/src/application/create_application.ts b/packages/core/src/application/create_application.ts index 83d276b11b1..4f18301dc9b 100644 --- a/packages/core/src/application/create_application.ts +++ b/packages/core/src/application/create_application.ts @@ -15,7 +15,7 @@ import {assertStandaloneComponentType} from '../render3/errors'; import {EnvironmentNgModuleRefAdapter} from '../render3/ng_module_ref'; import {NgZone} from '../zone/ng_zone'; -import {_callAndReportToErrorHandler, ApplicationRef} from './application_ref'; +import {ApplicationRef} from './application_ref'; import {ChangeDetectionScheduler} from '../change_detection/scheduling/zoneless_scheduling'; import {ChangeDetectionSchedulerImpl} from '../change_detection/scheduling/zoneless_scheduling_impl'; import {bootstrap} from '../platform/bootstrap'; diff --git a/packages/core/src/platform/bootstrap.ts b/packages/core/src/platform/bootstrap.ts index 3fdce7155df..017bc3f4e0a 100644 --- a/packages/core/src/platform/bootstrap.ts +++ b/packages/core/src/platform/bootstrap.ts @@ -20,11 +20,12 @@ import {setLocaleId} from '../render3/i18n/i18n_locale_id'; import {NgZone} from '../zone/ng_zone'; import {ApplicationInitStatus} from '../application/application_init'; -import {_callAndReportToErrorHandler, ApplicationRef, remove} from '../application/application_ref'; +import {ApplicationRef, remove} from '../application/application_ref'; import {PROVIDED_ZONELESS} from '../change_detection/scheduling/zoneless_scheduling'; import {InjectionToken, Injector} from '../di'; import {InternalNgModuleRef, NgModuleRef} from '../linker/ng_module_factory'; import {stringify} from '../util/stringify'; +import {isPromise} from '../util/lang'; /** * InjectionToken to control root component bootstrap behavior. @@ -200,3 +201,26 @@ function moduleDoBootstrap( } allPlatformModules.push(moduleRef); } + +function _callAndReportToErrorHandler( + errorHandler: ErrorHandler, + ngZone: NgZone, + callback: () => any, +): any { + try { + const result = callback(); + if (isPromise(result)) { + return result.catch((e: any) => { + ngZone.runOutsideAngular(() => errorHandler.handleError(e)); + // rethrow as the exception handler might not do it + throw e; + }); + } + + return result; + } catch (e) { + ngZone.runOutsideAngular(() => errorHandler.handleError(e)); + // rethrow as the exception handler might not do it + throw e; + } +} diff --git a/packages/core/src/platform/platform_ref.ts b/packages/core/src/platform/platform_ref.ts index 9ac30bacc75..86b6d7787ff 100644 --- a/packages/core/src/platform/platform_ref.ts +++ b/packages/core/src/platform/platform_ref.ts @@ -7,11 +7,7 @@ */ import {compileNgModuleFactory} from '../application/application_ngmodule_factory_compiler'; -import { - _callAndReportToErrorHandler, - BootstrapOptions, - optionsReducer, -} from '../application/application_ref'; +import {BootstrapOptions, optionsReducer} from '../application/application_ref'; import { getNgZoneOptions, internalProvideZoneChangeDetection,