From ad5fbd944dc1fdcf3721fd1bc45ba2929d264bd2 Mon Sep 17 00:00:00 2001 From: arturovt Date: Wed, 2 Apr 2025 18:55:41 +0300 Subject: [PATCH] refactor(common): drop value not a number message in production (#60699) Drops value not a number message in production. PR Close #60699 --- goldens/public-api/common/errors.api.md | 4 +++- packages/common/src/errors.ts | 1 + packages/common/src/pipes/number_pipe.ts | 17 ++++++++++++++--- packages/common/test/pipes/number_pipe_spec.ts | 8 ++++---- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/goldens/public-api/common/errors.api.md b/goldens/public-api/common/errors.api.md index 6afb75a0e42..f93c0f8544f 100644 --- a/goldens/public-api/common/errors.api.md +++ b/goldens/public-api/common/errors.api.md @@ -71,7 +71,9 @@ export const enum RuntimeErrorCode { // (undocumented) UNKNOWN_DATE_TYPE_VALUE = 2301, // (undocumented) - UNKNOWN_ZONE_WIDTH = 2302 + UNKNOWN_ZONE_WIDTH = 2302, + // (undocumented) + VALUE_NOT_A_NUMBER = 2309 } // (No @packageDocumentation comment for this package) diff --git a/packages/common/src/errors.ts b/packages/common/src/errors.ts index a7f95c37ce5..72e02440d39 100644 --- a/packages/common/src/errors.ts +++ b/packages/common/src/errors.ts @@ -36,6 +36,7 @@ export const enum RuntimeErrorCode { INVALID_DIGIT_INFO = 2306, INVALID_NUMBER_OF_DIGITS_AFTER_FRACTION = 2307, NO_PLURAL_MESSAGE_FOUND = 2308, + VALUE_NOT_A_NUMBER = 2309, // Keep 2800 - 2900 for Http Errors. diff --git a/packages/common/src/pipes/number_pipe.ts b/packages/common/src/pipes/number_pipe.ts index a4bba3b6070..174b1acc801 100644 --- a/packages/common/src/pipes/number_pipe.ts +++ b/packages/common/src/pipes/number_pipe.ts @@ -6,12 +6,20 @@ * found in the LICENSE file at https://angular.dev/license */ -import {DEFAULT_CURRENCY_CODE, Inject, LOCALE_ID, Pipe, PipeTransform} from '@angular/core'; +import { + DEFAULT_CURRENCY_CODE, + Inject, + LOCALE_ID, + Pipe, + PipeTransform, + ɵRuntimeError as RuntimeError, +} from '@angular/core'; import {formatCurrency, formatNumber, formatPercent} from '../i18n/format_number'; import {getCurrencySymbol} from '../i18n/locale_data_api'; import {invalidPipeArgumentError} from './invalid_pipe_argument_error'; +import {RuntimeErrorCode} from '../errors'; /** * @ngModule CommonModule @@ -274,7 +282,7 @@ export class CurrencyPipe implements PipeTransform { locale ||= this._locale; if (typeof display === 'boolean') { - if ((typeof ngDevMode === 'undefined' || ngDevMode) && console && console.warn) { + if (typeof ngDevMode === 'undefined' || ngDevMode) { console.warn( `Warning: the currency pipe has been changed in Angular v5. The symbolDisplay option (third parameter) is now a string instead of a boolean. The accepted values are "code", "symbol" or "symbol-narrow".`, ); @@ -313,7 +321,10 @@ function strToNumber(value: number | string): number { return Number(value); } if (typeof value !== 'number') { - throw new Error(`${value} is not a number`); + throw new RuntimeError( + RuntimeErrorCode.VALUE_NOT_A_NUMBER, + ngDevMode && `${value} is not a number`, + ); } return value; } diff --git a/packages/common/test/pipes/number_pipe_spec.ts b/packages/common/test/pipes/number_pipe_spec.ts index 8f3963df64b..11acdb05396 100644 --- a/packages/common/test/pipes/number_pipe_spec.ts +++ b/packages/common/test/pipes/number_pipe_spec.ts @@ -63,10 +63,10 @@ describe('Number pipes', () => { it('should not support other objects', () => { expect(() => pipe.transform({} as any)).toThrowError( - `NG02100: InvalidPipeArgument: '[object Object] is not a number' for pipe 'DecimalPipe'`, + `NG02100: InvalidPipeArgument: 'NG02309: [object Object] is not a number' for pipe 'DecimalPipe'`, ); expect(() => pipe.transform('123abc')).toThrowError( - `NG02100: InvalidPipeArgument: '123abc is not a number' for pipe 'DecimalPipe'`, + `NG02100: InvalidPipeArgument: 'NG02309: 123abc is not a number' for pipe 'DecimalPipe'`, ); }); }); @@ -125,7 +125,7 @@ describe('Number pipes', () => { it('should not support other objects', () => { expect(() => pipe.transform({} as any)).toThrowError( - `NG02100: InvalidPipeArgument: '[object Object] is not a number' for pipe 'PercentPipe'`, + `NG02100: InvalidPipeArgument: 'NG02309: [object Object] is not a number' for pipe 'PercentPipe'`, ); }); }); @@ -214,7 +214,7 @@ describe('Number pipes', () => { it('should not support other objects', () => { expect(() => pipe.transform({} as any)).toThrowError( - `NG02100: InvalidPipeArgument: '[object Object] is not a number' for pipe 'CurrencyPipe'`, + `NG02100: InvalidPipeArgument: 'NG02309: [object Object] is not a number' for pipe 'CurrencyPipe'`, ); });