Skip to content

Commit

Permalink
refactor(common): make the error messages tree shakable (#44663)
Browse files Browse the repository at this point in the history
Make Long error messages tree-shakable in the production build with error codes.

fixes #40096

PR Close #44663
  • Loading branch information
ramthir authored and dylhunn committed Jan 18, 2022
1 parent ccb73c0 commit 4307b82
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
2 changes: 2 additions & 0 deletions goldens/public-api/common/errors.md
Expand Up @@ -6,6 +6,8 @@

// @public
export const enum RuntimeErrorCode {
// (undocumented)
INVALID_PIPE_ARGUMENT = 2100,
// (undocumented)
PARENT_NG_SWITCH_NOT_FOUND = 2000
}
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/errors.ts
Expand Up @@ -13,4 +13,6 @@
export const enum RuntimeErrorCode {
// NgSwitch errors
PARENT_NG_SWITCH_NOT_FOUND = 2000,
// Pipe errors
INVALID_PIPE_ARGUMENT = 2100
}
9 changes: 7 additions & 2 deletions packages/common/src/pipes/invalid_pipe_argument_error.ts
Expand Up @@ -6,8 +6,13 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Type, ɵstringify as stringify} from '@angular/core';
import {Type, ɵRuntimeError as RuntimeError, ɵstringify as stringify} from '@angular/core';

import {RuntimeErrorCode} from '../errors';

export function invalidPipeArgumentError(type: Type<any>, value: Object) {
return Error(`InvalidPipeArgument: '${value}' for pipe '${stringify(type)}'`);
const errorMessage = (typeof ngDevMode === 'undefined' || ngDevMode) ?
`InvalidPipeArgument: '${value}' for pipe '${stringify(type)}'` :
'';
return new RuntimeError(RuntimeErrorCode.INVALID_PIPE_ARGUMENT, errorMessage);
}
9 changes: 5 additions & 4 deletions packages/common/test/pipes/number_pipe_spec.ts
Expand Up @@ -64,9 +64,10 @@ import {ɵregisterLocaleData, ɵunregisterLocaleData} from '@angular/core';
it('should not support other objects', () => {
expect(() => pipe.transform({} as any))
.toThrowError(
`InvalidPipeArgument: '[object Object] is not a number' for pipe 'DecimalPipe'`);
`NG02100: InvalidPipeArgument: '[object Object] is not a number' for pipe 'DecimalPipe'`);
expect(() => pipe.transform('123abc'))
.toThrowError(`InvalidPipeArgument: '123abc is not a number' for pipe 'DecimalPipe'`);
.toThrowError(
`NG02100: InvalidPipeArgument: '123abc is not a number' for pipe 'DecimalPipe'`);
});
});

Expand Down Expand Up @@ -108,7 +109,7 @@ import {ɵregisterLocaleData, ɵunregisterLocaleData} from '@angular/core';
it('should not support other objects', () => {
expect(() => pipe.transform({} as any))
.toThrowError(
`InvalidPipeArgument: '[object Object] is not a number' for pipe 'PercentPipe'`);
`NG02100: InvalidPipeArgument: '[object Object] is not a number' for pipe 'PercentPipe'`);
});
});
});
Expand Down Expand Up @@ -168,7 +169,7 @@ import {ɵregisterLocaleData, ɵunregisterLocaleData} from '@angular/core';
it('should not support other objects', () => {
expect(() => pipe.transform({} as any))
.toThrowError(
`InvalidPipeArgument: '[object Object] is not a number' for pipe 'CurrencyPipe'`);
`NG02100: InvalidPipeArgument: '[object Object] is not a number' for pipe 'CurrencyPipe'`);
});

it('should warn if you are using the v4 signature', () => {
Expand Down
Expand Up @@ -1878,7 +1878,7 @@
"name": "stringify"
},
{
"name": "stringify6"
"name": "stringify7"
},
{
"name": "stringifyCSSSelector"
Expand Down

0 comments on commit 4307b82

Please sign in to comment.