Skip to content

Commit 5317979

Browse files
arturovtthePunderWoman
authored andcommitted
refactor(common): drop error messages in production (angular#60415)
Drops more error messages in production. PR Close angular#60415
1 parent 43cbc58 commit 5317979

File tree

6 files changed

+52
-12
lines changed

6 files changed

+52
-12
lines changed

goldens/public-api/common/errors.api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ export const enum RuntimeErrorCode {
99
// (undocumented)
1010
EQUALITY_NG_SWITCH_DIFFERENCE = 2001,
1111
// (undocumented)
12+
INVALID_DIGIT_INFO = 2306,
13+
// (undocumented)
1214
INVALID_INPUT = 2952,
1315
// (undocumented)
16+
INVALID_INTEGER_LITERAL = 2305,
17+
// (undocumented)
1418
INVALID_LOADER_ARGUMENTS = 2959,
1519
// (undocumented)
20+
INVALID_NUMBER_OF_DIGITS_AFTER_FRACTION = 2307,
21+
// (undocumented)
1622
INVALID_PIPE_ARGUMENT = 2100,
1723
// (undocumented)
1824
INVALID_TO_DATE_CONVERSION = 2302,
@@ -21,14 +27,20 @@ export const enum RuntimeErrorCode {
2127
// (undocumented)
2228
LCP_IMG_NGSRC_MODIFIED = 2964,
2329
// (undocumented)
30+
LOCALE_DATA_UNDEFINED = 2304,
31+
// (undocumented)
2432
MISSING_BUILTIN_LOADER = 2962,
2533
// (undocumented)
34+
MISSING_EXTRA_LOCALE_DATA_FOR_LOCALE = 2303,
35+
// (undocumented)
2636
MISSING_NECESSARY_LOADER = 2963,
2737
// (undocumented)
2838
NG_FOR_MISSING_DIFFER = -2200,
2939
// (undocumented)
3040
NG_IF_NOT_A_TEMPLATE_REF = 2020,
3141
// (undocumented)
42+
NO_PLURAL_MESSAGE_FOUND = 2308,
43+
// (undocumented)
3244
OVERSIZED_IMAGE = 2960,
3345
// (undocumented)
3446
OVERSIZED_PLACEHOLDER = 2965,

packages/common/src/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export const enum RuntimeErrorCode {
3030
UNEXPECTED_TRANSLATION_TYPE = 2302,
3131
UNKNOWN_ZONE_WIDTH = 2302,
3232
INVALID_TO_DATE_CONVERSION = 2302,
33+
MISSING_EXTRA_LOCALE_DATA_FOR_LOCALE = 2303,
34+
LOCALE_DATA_UNDEFINED = 2304,
35+
INVALID_INTEGER_LITERAL = 2305,
36+
INVALID_DIGIT_INFO = 2306,
37+
INVALID_NUMBER_OF_DIGITS_AFTER_FRACTION = 2307,
38+
NO_PLURAL_MESSAGE_FOUND = 2308,
3339

3440
// Keep 2800 - 2900 for Http Errors.
3541

packages/common/src/i18n/format_number.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import {ɵRuntimeError as RuntimeError} from '@angular/core';
10+
911
import {
1012
getLocaleNumberFormat,
1113
getLocaleNumberSymbol,
1214
getNumberOfCurrencyDigits,
1315
NumberFormatStyle,
1416
NumberSymbol,
1517
} from './locale_data_api';
18+
import {RuntimeErrorCode} from '../errors';
1619

1720
export const NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(-(\d+))?)?$/;
1821
const MAX_DIGITS = 22;
@@ -55,7 +58,10 @@ function formatNumberToLocaleString(
5558
if (digitsInfo) {
5659
const parts = digitsInfo.match(NUMBER_FORMAT_REGEXP);
5760
if (parts === null) {
58-
throw new Error(`${digitsInfo} is not a valid digit info`);
61+
throw new RuntimeError(
62+
RuntimeErrorCode.INVALID_DIGIT_INFO,
63+
ngDevMode && `${digitsInfo} is not a valid digit info`,
64+
);
5965
}
6066
const minIntPart = parts[1];
6167
const minFractionPart = parts[3];
@@ -436,8 +442,10 @@ function parseNumber(num: number): ParsedNumber {
436442
*/
437443
function roundNumber(parsedNumber: ParsedNumber, minFrac: number, maxFrac: number) {
438444
if (minFrac > maxFrac) {
439-
throw new Error(
440-
`The minimum number of digits after fraction (${minFrac}) is higher than the maximum (${maxFrac}).`,
445+
throw new RuntimeError(
446+
RuntimeErrorCode.INVALID_NUMBER_OF_DIGITS_AFTER_FRACTION,
447+
ngDevMode &&
448+
`The minimum number of digits after fraction (${minFrac}) is higher than the maximum (${maxFrac}).`,
441449
);
442450
}
443451

@@ -509,7 +517,10 @@ function roundNumber(parsedNumber: ParsedNumber, minFrac: number, maxFrac: numbe
509517
export function parseIntAutoRadix(text: string): number {
510518
const result: number = parseInt(text);
511519
if (isNaN(result)) {
512-
throw new Error('Invalid integer literal when parsing ' + text);
520+
throw new RuntimeError(
521+
RuntimeErrorCode.INVALID_INTEGER_LITERAL,
522+
ngDevMode && 'Invalid integer literal when parsing ' + text,
523+
);
513524
}
514525
return result;
515526
}

packages/common/src/i18n/locale_data_api.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import {
1313
ɵgetLocaleCurrencyCode,
1414
ɵgetLocalePluralCase,
1515
ɵLocaleDataIndex,
16+
ɵRuntimeError as RuntimeError,
1617
} from '@angular/core';
1718

1819
import {CURRENCIES_EN, CurrenciesSymbols} from './currencies';
20+
import {RuntimeErrorCode} from '../errors';
1921

2022
/**
2123
* Format styles that can be used to represent numbers.
@@ -599,10 +601,12 @@ export const getLocalePluralCase: (locale: string) => (value: number) => Plural
599601

600602
function checkFullData(data: any) {
601603
if (!data[ɵLocaleDataIndex.ExtraData]) {
602-
throw new Error(
603-
`Missing extra locale data for the locale "${
604-
data[ɵLocaleDataIndex.LocaleId]
605-
}". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`,
604+
throw new RuntimeError(
605+
RuntimeErrorCode.MISSING_EXTRA_LOCALE_DATA_FOR_LOCALE,
606+
ngDevMode &&
607+
`Missing extra locale data for the locale "${
608+
data[ɵLocaleDataIndex.LocaleId]
609+
}". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`,
606610
);
607611
}
608612
}
@@ -715,7 +719,10 @@ function getLastDefinedValue<T>(data: T[], index: number): T {
715719
return data[i];
716720
}
717721
}
718-
throw new Error('Locale data API: locale data undefined');
722+
throw new RuntimeError(
723+
RuntimeErrorCode.LOCALE_DATA_UNDEFINED,
724+
ngDevMode && 'Locale data API: locale data undefined',
725+
);
719726
}
720727

721728
/**

packages/common/src/i18n/localization.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {Inject, Injectable, LOCALE_ID} from '@angular/core';
9+
import {Inject, Injectable, LOCALE_ID, ɵRuntimeError as RuntimeError} from '@angular/core';
1010

1111
import {getLocalePluralCase, Plural} from './locale_data_api';
12+
import {RuntimeErrorCode} from '../errors';
1213

1314
/**
1415
* @publicApi
@@ -49,7 +50,10 @@ export function getPluralCategory(
4950
return 'other';
5051
}
5152

52-
throw new Error(`No plural message found for value "${value}"`);
53+
throw new RuntimeError(
54+
RuntimeErrorCode.NO_PLURAL_MESSAGE_FOUND,
55+
ngDevMode && `No plural message found for value "${value}"`,
56+
);
5357
}
5458

5559
/**

packages/common/test/i18n/localization_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ describe('l10n', () => {
188188
const l10n = new NgLocaleLocalization('ro');
189189
// 2 -> 'few'
190190
getPluralCategory(2, ['one'], l10n);
191-
}).toThrowError('No plural message found for value "2"');
191+
}).toThrowError('NG02308: No plural message found for value "2"');
192192
});
193193
});
194194
});

0 commit comments

Comments
 (0)