From 5317979738cb90f4372baacc417693cd9bf4fa52 Mon Sep 17 00:00:00 2001 From: arturovt Date: Mon, 17 Mar 2025 11:18:13 +0200 Subject: [PATCH] refactor(common): drop error messages in production (#60415) Drops more error messages in production. PR Close #60415 --- goldens/public-api/common/errors.api.md | 12 ++++++++++++ packages/common/src/errors.ts | 6 ++++++ packages/common/src/i18n/format_number.ts | 19 +++++++++++++++---- packages/common/src/i18n/locale_data_api.ts | 17 ++++++++++++----- packages/common/src/i18n/localization.ts | 8 ++++++-- .../common/test/i18n/localization_spec.ts | 2 +- 6 files changed, 52 insertions(+), 12 deletions(-) diff --git a/goldens/public-api/common/errors.api.md b/goldens/public-api/common/errors.api.md index a0f8532f42b..6afb75a0e42 100644 --- a/goldens/public-api/common/errors.api.md +++ b/goldens/public-api/common/errors.api.md @@ -9,10 +9,16 @@ export const enum RuntimeErrorCode { // (undocumented) EQUALITY_NG_SWITCH_DIFFERENCE = 2001, // (undocumented) + INVALID_DIGIT_INFO = 2306, + // (undocumented) INVALID_INPUT = 2952, // (undocumented) + INVALID_INTEGER_LITERAL = 2305, + // (undocumented) INVALID_LOADER_ARGUMENTS = 2959, // (undocumented) + INVALID_NUMBER_OF_DIGITS_AFTER_FRACTION = 2307, + // (undocumented) INVALID_PIPE_ARGUMENT = 2100, // (undocumented) INVALID_TO_DATE_CONVERSION = 2302, @@ -21,14 +27,20 @@ export const enum RuntimeErrorCode { // (undocumented) LCP_IMG_NGSRC_MODIFIED = 2964, // (undocumented) + LOCALE_DATA_UNDEFINED = 2304, + // (undocumented) MISSING_BUILTIN_LOADER = 2962, // (undocumented) + MISSING_EXTRA_LOCALE_DATA_FOR_LOCALE = 2303, + // (undocumented) MISSING_NECESSARY_LOADER = 2963, // (undocumented) NG_FOR_MISSING_DIFFER = -2200, // (undocumented) NG_IF_NOT_A_TEMPLATE_REF = 2020, // (undocumented) + NO_PLURAL_MESSAGE_FOUND = 2308, + // (undocumented) OVERSIZED_IMAGE = 2960, // (undocumented) OVERSIZED_PLACEHOLDER = 2965, diff --git a/packages/common/src/errors.ts b/packages/common/src/errors.ts index a8e83cc12e9..a7f95c37ce5 100644 --- a/packages/common/src/errors.ts +++ b/packages/common/src/errors.ts @@ -30,6 +30,12 @@ export const enum RuntimeErrorCode { UNEXPECTED_TRANSLATION_TYPE = 2302, UNKNOWN_ZONE_WIDTH = 2302, INVALID_TO_DATE_CONVERSION = 2302, + MISSING_EXTRA_LOCALE_DATA_FOR_LOCALE = 2303, + LOCALE_DATA_UNDEFINED = 2304, + INVALID_INTEGER_LITERAL = 2305, + INVALID_DIGIT_INFO = 2306, + INVALID_NUMBER_OF_DIGITS_AFTER_FRACTION = 2307, + NO_PLURAL_MESSAGE_FOUND = 2308, // Keep 2800 - 2900 for Http Errors. diff --git a/packages/common/src/i18n/format_number.ts b/packages/common/src/i18n/format_number.ts index f83f22d00d9..252593e3881 100644 --- a/packages/common/src/i18n/format_number.ts +++ b/packages/common/src/i18n/format_number.ts @@ -6,6 +6,8 @@ * found in the LICENSE file at https://angular.dev/license */ +import {ɵRuntimeError as RuntimeError} from '@angular/core'; + import { getLocaleNumberFormat, getLocaleNumberSymbol, @@ -13,6 +15,7 @@ import { NumberFormatStyle, NumberSymbol, } from './locale_data_api'; +import {RuntimeErrorCode} from '../errors'; export const NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(-(\d+))?)?$/; const MAX_DIGITS = 22; @@ -55,7 +58,10 @@ function formatNumberToLocaleString( if (digitsInfo) { const parts = digitsInfo.match(NUMBER_FORMAT_REGEXP); if (parts === null) { - throw new Error(`${digitsInfo} is not a valid digit info`); + throw new RuntimeError( + RuntimeErrorCode.INVALID_DIGIT_INFO, + ngDevMode && `${digitsInfo} is not a valid digit info`, + ); } const minIntPart = parts[1]; const minFractionPart = parts[3]; @@ -436,8 +442,10 @@ function parseNumber(num: number): ParsedNumber { */ function roundNumber(parsedNumber: ParsedNumber, minFrac: number, maxFrac: number) { if (minFrac > maxFrac) { - throw new Error( - `The minimum number of digits after fraction (${minFrac}) is higher than the maximum (${maxFrac}).`, + throw new RuntimeError( + RuntimeErrorCode.INVALID_NUMBER_OF_DIGITS_AFTER_FRACTION, + ngDevMode && + `The minimum number of digits after fraction (${minFrac}) is higher than the maximum (${maxFrac}).`, ); } @@ -509,7 +517,10 @@ function roundNumber(parsedNumber: ParsedNumber, minFrac: number, maxFrac: numbe export function parseIntAutoRadix(text: string): number { const result: number = parseInt(text); if (isNaN(result)) { - throw new Error('Invalid integer literal when parsing ' + text); + throw new RuntimeError( + RuntimeErrorCode.INVALID_INTEGER_LITERAL, + ngDevMode && 'Invalid integer literal when parsing ' + text, + ); } return result; } diff --git a/packages/common/src/i18n/locale_data_api.ts b/packages/common/src/i18n/locale_data_api.ts index 574ec470faa..cb741aa9936 100644 --- a/packages/common/src/i18n/locale_data_api.ts +++ b/packages/common/src/i18n/locale_data_api.ts @@ -13,9 +13,11 @@ import { ɵgetLocaleCurrencyCode, ɵgetLocalePluralCase, ɵLocaleDataIndex, + ɵRuntimeError as RuntimeError, } from '@angular/core'; import {CURRENCIES_EN, CurrenciesSymbols} from './currencies'; +import {RuntimeErrorCode} from '../errors'; /** * Format styles that can be used to represent numbers. @@ -599,10 +601,12 @@ export const getLocalePluralCase: (locale: string) => (value: number) => Plural function checkFullData(data: any) { if (!data[ɵLocaleDataIndex.ExtraData]) { - throw new Error( - `Missing extra locale data for the locale "${ - data[ɵLocaleDataIndex.LocaleId] - }". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`, + throw new RuntimeError( + RuntimeErrorCode.MISSING_EXTRA_LOCALE_DATA_FOR_LOCALE, + ngDevMode && + `Missing extra locale data for the locale "${ + data[ɵLocaleDataIndex.LocaleId] + }". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`, ); } } @@ -715,7 +719,10 @@ function getLastDefinedValue(data: T[], index: number): T { return data[i]; } } - throw new Error('Locale data API: locale data undefined'); + throw new RuntimeError( + RuntimeErrorCode.LOCALE_DATA_UNDEFINED, + ngDevMode && 'Locale data API: locale data undefined', + ); } /** diff --git a/packages/common/src/i18n/localization.ts b/packages/common/src/i18n/localization.ts index 0603e01b7b2..2740d41d5e0 100644 --- a/packages/common/src/i18n/localization.ts +++ b/packages/common/src/i18n/localization.ts @@ -6,9 +6,10 @@ * found in the LICENSE file at https://angular.dev/license */ -import {Inject, Injectable, LOCALE_ID} from '@angular/core'; +import {Inject, Injectable, LOCALE_ID, ɵRuntimeError as RuntimeError} from '@angular/core'; import {getLocalePluralCase, Plural} from './locale_data_api'; +import {RuntimeErrorCode} from '../errors'; /** * @publicApi @@ -49,7 +50,10 @@ export function getPluralCategory( return 'other'; } - throw new Error(`No plural message found for value "${value}"`); + throw new RuntimeError( + RuntimeErrorCode.NO_PLURAL_MESSAGE_FOUND, + ngDevMode && `No plural message found for value "${value}"`, + ); } /** diff --git a/packages/common/test/i18n/localization_spec.ts b/packages/common/test/i18n/localization_spec.ts index 0addffebfb3..3005245597c 100644 --- a/packages/common/test/i18n/localization_spec.ts +++ b/packages/common/test/i18n/localization_spec.ts @@ -188,7 +188,7 @@ describe('l10n', () => { const l10n = new NgLocaleLocalization('ro'); // 2 -> 'few' getPluralCategory(2, ['one'], l10n); - }).toThrowError('No plural message found for value "2"'); + }).toThrowError('NG02308: No plural message found for value "2"'); }); }); });