Skip to content

Commit 96dfc7a

Browse files
ramthirdylhunn
authored andcommitted
refactor(core): make the error messages tree shakable (#44219)
Long error messages can be tree-shaken in the production build. Doing this, we will keep the throw and remove the long error messages. See: #44210 (review) PR Close #44219
1 parent ca5b9b5 commit 96dfc7a

File tree

5 files changed

+59
-30
lines changed

5 files changed

+59
-30
lines changed

goldens/size-tracking/aio-payloads.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
"master": {
1616
"uncompressed": {
1717
"runtime": 4436,
18-
"main": 459822,
18+
"main": 459047,
1919
"polyfills": 37271,
2020
"styles": 70719,
2121
"light-theme": 77717,
2222
"dark-theme": 77897
2323
}
2424
}
2525
}
26-
}
26+
}

goldens/size-tracking/integration-payloads.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"master": {
44
"uncompressed": {
55
"runtime": 1083,
6-
"main": 138745,
7-
"polyfills": 37226
6+
"main": 138491,
7+
"polyfills": 36964
88
}
99
}
1010
},
@@ -24,7 +24,7 @@
2424
"master": {
2525
"uncompressed": {
2626
"runtime": 1105,
27-
"main": 144744,
27+
"main": 144185,
2828
"polyfills": 36939
2929
}
3030
}
@@ -33,7 +33,7 @@
3333
"master": {
3434
"uncompressed": {
3535
"runtime": 929,
36-
"main": 137444,
36+
"main": 136770,
3737
"polyfills": 37933
3838
}
3939
}
@@ -42,7 +42,7 @@
4242
"master": {
4343
"uncompressed": {
4444
"runtime": 2843,
45-
"main": 231164,
45+
"main": 230506,
4646
"polyfills": 37064,
4747
"src_app_lazy_lazy_module_ts": 795
4848
}
@@ -52,7 +52,7 @@
5252
"master": {
5353
"uncompressed": {
5454
"runtime": 1063,
55-
"main": 164069,
55+
"main": 163342,
5656
"polyfills": 36975
5757
}
5858
}
@@ -68,4 +68,4 @@
6868
}
6969
}
7070
}
71-
}
71+
}

packages/core/src/application_ref.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {InternalViewRef, ViewRef} from './linker/view_ref';
3232
import {isComponentResourceResolutionQueueEmpty, resolveComponentResources} from './metadata/resource_loading';
3333
import {assertNgModuleType} from './render3/assert';
3434
import {ComponentFactory as R3ComponentFactory} from './render3/component_ref';
35+
import {RuntimeError, RuntimeErrorCode} from './render3/error_code';
3536
import {setLocaleId} from './render3/i18n/i18n_locale_id';
3637
import {setJitOptions} from './render3/jit/jit_options';
3738
import {NgModuleFactory as R3NgModuleFactory} from './render3/ng_module_ref';
@@ -123,8 +124,10 @@ export class NgProbeToken {
123124
export function createPlatform(injector: Injector): PlatformRef {
124125
if (_platform && !_platform.destroyed &&
125126
!_platform.injector.get(ALLOW_MULTIPLE_PLATFORMS, false)) {
126-
throw new Error(
127-
'There can be only one platform. Destroy the previous one to create a new one.');
127+
const errorMessage = (typeof ngDevMode === 'undefined' || ngDevMode) ?
128+
'There can be only one platform. Destroy the previous one to create a new one.' :
129+
'';
130+
throw new RuntimeError(RuntimeErrorCode.MULTIPLE_PLATFORMS, errorMessage);
128131
}
129132
publishDefaultGlobalUtils();
130133
_platform = injector.get(PlatformRef);
@@ -177,11 +180,15 @@ export function assertPlatform(requiredToken: any): PlatformRef {
177180
const platform = getPlatform();
178181

179182
if (!platform) {
180-
throw new Error('No platform exists!');
183+
const errorMessage =
184+
(typeof ngDevMode === 'undefined' || ngDevMode) ? 'No platform exists!' : '';
185+
throw new RuntimeError(RuntimeErrorCode.PLATFORM_NOT_FOUND, errorMessage);
181186
}
182187

183-
if (!platform.injector.get(requiredToken, null)) {
184-
throw new Error(
188+
if ((typeof ngDevMode === 'undefined' || ngDevMode) &&
189+
!platform.injector.get(requiredToken, null)) {
190+
throw new RuntimeError(
191+
RuntimeErrorCode.MULTIPLE_PLATFORMS,
185192
'A platform with a different configuration has been created. Please destroy it first.');
186193
}
187194

@@ -329,7 +336,10 @@ export class PlatformRef {
329336
const moduleRef = <InternalNgModuleRef<M>>moduleFactory.create(ngZoneInjector);
330337
const exceptionHandler: ErrorHandler|null = moduleRef.injector.get(ErrorHandler, null);
331338
if (!exceptionHandler) {
332-
throw new Error('No ErrorHandler. Is platform module (BrowserModule) included?');
339+
const errorMessage = (typeof ngDevMode === 'undefined' || ngDevMode) ?
340+
'No ErrorHandler. Is platform module (BrowserModule) included?' :
341+
'';
342+
throw new RuntimeError(RuntimeErrorCode.ERROR_HANDLER_NOT_FOUND, errorMessage);
333343
}
334344
ngZone!.runOutsideAngular(() => {
335345
const subscription = ngZone!.onError.subscribe({
@@ -388,12 +398,12 @@ export class PlatformRef {
388398
} else if (moduleRef.instance.ngDoBootstrap) {
389399
moduleRef.instance.ngDoBootstrap(appRef);
390400
} else {
391-
throw new Error(
392-
`The module ${
393-
stringify(
394-
moduleRef.instance
395-
.constructor)} was bootstrapped, but it does not declare "@NgModule.bootstrap" components nor a "ngDoBootstrap" method. ` +
396-
`Please define one of these.`);
401+
const errorMessage = (typeof ngDevMode === 'undefined' || ngDevMode) ?
402+
`The module ${stringify(moduleRef.instance.constructor)} was bootstrapped, ` +
403+
`but it does not declare "@NgModule.bootstrap" components nor a "ngDoBootstrap" method. ` +
404+
`Please define one of these.` :
405+
'';
406+
throw new RuntimeError(RuntimeErrorCode.BOOTSTRAP_COMPONENTS_NOT_FOUND, errorMessage);
397407
}
398408
this._modules.push(moduleRef);
399409
}
@@ -419,7 +429,10 @@ export class PlatformRef {
419429
*/
420430
destroy() {
421431
if (this._destroyed) {
422-
throw new Error('The platform has already been destroyed!');
432+
const errorMessage = (typeof ngDevMode === 'undefined' || ngDevMode) ?
433+
'The platform has already been destroyed!' :
434+
'';
435+
throw new RuntimeError(RuntimeErrorCode.ALREADY_DESTROYED_PLATFORM, errorMessage);
423436
}
424437
this._modules.slice().forEach(module => module.destroy());
425438
this._destroyListeners.forEach(listener => listener());
@@ -782,8 +795,11 @@ export class ApplicationRef {
782795
bootstrap<C>(componentOrFactory: ComponentFactory<C>|Type<C>, rootSelectorOrNode?: string|any):
783796
ComponentRef<C> {
784797
if (!this._initStatus.done) {
785-
throw new Error(
786-
'Cannot bootstrap as there are still asynchronous initializers running. Bootstrap components in the `ngDoBootstrap` method of the root module.');
798+
const errorMessage = (typeof ngDevMode === 'undefined' || ngDevMode) ?
799+
'Cannot bootstrap as there are still asynchronous initializers running. ' +
800+
'Bootstrap components in the `ngDoBootstrap` method of the root module.' :
801+
'';
802+
throw new RuntimeError(RuntimeErrorCode.ASYNC_INITIALIZERS_STILL_RUNNING, errorMessage);
787803
}
788804
let componentFactory: ComponentFactory<C>;
789805
if (componentOrFactory instanceof ComponentFactory) {
@@ -835,7 +851,10 @@ export class ApplicationRef {
835851
*/
836852
tick(): void {
837853
if (this._runningTick) {
838-
throw new Error('ApplicationRef.tick is called recursively');
854+
const errorMessage = (typeof ngDevMode === 'undefined' || ngDevMode) ?
855+
'ApplicationRef.tick is called recursively' :
856+
'';
857+
throw new RuntimeError(RuntimeErrorCode.RECURSIVE_APPLICATION_REF_TICK, errorMessage);
839858
}
840859

841860
try {

packages/core/src/render3/error_code.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const enum RuntimeErrorCode {
1313

1414
// Change Detection Errors
1515
EXPRESSION_CHANGED_AFTER_CHECKED = '100',
16+
RECURSIVE_APPLICATION_REF_TICK = '101',
1617

1718
// Dependency Injection Errors
1819
CYCLIC_DI_DEPENDENCY = '200',
@@ -24,7 +25,15 @@ export const enum RuntimeErrorCode {
2425
PIPE_NOT_FOUND = '302',
2526
UNKNOWN_BINDING = '303',
2627
UNKNOWN_ELEMENT = '304',
27-
TEMPLATE_STRUCTURE_ERROR = '305'
28+
TEMPLATE_STRUCTURE_ERROR = '305',
29+
30+
// Bootstrap Errors
31+
MULTIPLE_PLATFORMS = '400',
32+
PLATFORM_NOT_FOUND = '401',
33+
ERROR_HANDLER_NOT_FOUND = '402',
34+
BOOTSTRAP_COMPONENTS_NOT_FOUND = '403',
35+
ALREADY_DESTROYED_PLATFORM = '404',
36+
ASYNC_INITIALIZERS_STILL_RUNNING = '405',
2837

2938
// Styling Errors
3039

packages/core/test/application_ref_spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class SomeComponent {
166166
appRef.attachView(fixture.componentRef.hostView);
167167
appRef.tick();
168168
expect(fixture.componentInstance.reenterErr.message)
169-
.toBe('ApplicationRef.tick is called recursively');
169+
.toBe('NG0101: ApplicationRef.tick is called recursively');
170170
});
171171

172172
describe('APP_BOOTSTRAP_LISTENER', () => {
@@ -204,7 +204,7 @@ class SomeComponent {
204204
createRootEl();
205205
expect(() => ref.bootstrap(SomeComponent))
206206
.toThrowError(
207-
'Cannot bootstrap as there are still asynchronous initializers running. Bootstrap components in the `ngDoBootstrap` method of the root module.');
207+
'NG0405: Cannot bootstrap as there are still asynchronous initializers running. Bootstrap components in the `ngDoBootstrap` method of the root module.');
208208
})));
209209
});
210210
});
@@ -273,7 +273,8 @@ class SomeComponent {
273273
return defaultPlatform.bootstrapModule(EmptyModule)
274274
.then(() => fail('expecting error'), (error) => {
275275
expect(error.message)
276-
.toEqual('No ErrorHandler. Is platform module (BrowserModule) included?');
276+
.toEqual(
277+
'NG0402: No ErrorHandler. Is platform module (BrowserModule) included?');
277278
});
278279
}));
279280

@@ -300,7 +301,7 @@ class SomeComponent {
300301
defaultPlatform.bootstrapModule(createModule({ngDoBootstrap: false}))
301302
.then(() => expect(false).toBe(true), (e) => {
302303
const expectedErrMsg =
303-
`The module MyModule was bootstrapped, but it does not declare "@NgModule.bootstrap" components nor a "ngDoBootstrap" method. Please define one of these.`;
304+
`NG0403: The module MyModule was bootstrapped, but it does not declare "@NgModule.bootstrap" components nor a "ngDoBootstrap" method. Please define one of these.`;
304305
expect(e.message).toEqual(expectedErrMsg);
305306
expect(mockConsole.res[0].join('#')).toEqual('ERROR#Error: ' + expectedErrMsg);
306307
});

0 commit comments

Comments
 (0)