Skip to content

Commit 3602c53

Browse files
arturovtAndrewKushnir
authored andcommitted
refactor(common): drop error messages in production (angular#60242)
Drops more error messages in production. PR Close angular#60242
1 parent 9be2b33 commit 3602c53

File tree

7 files changed

+57
-14
lines changed

7 files changed

+57
-14
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export const enum RuntimeErrorCode {
1515
// (undocumented)
1616
INVALID_PIPE_ARGUMENT = 2100,
1717
// (undocumented)
18+
INVALID_TO_DATE_CONVERSION = 2302,
19+
// (undocumented)
1820
LCP_IMG_MISSING_PRIORITY = 2955,
1921
// (undocumented)
2022
LCP_IMG_NGSRC_MODIFIED = 2964,
@@ -51,7 +53,13 @@ export const enum RuntimeErrorCode {
5153
// (undocumented)
5254
UNEXPECTED_SRC_ATTR = 2950,
5355
// (undocumented)
54-
UNEXPECTED_SRCSET_ATTR = 2951
56+
UNEXPECTED_SRCSET_ATTR = 2951,
57+
// (undocumented)
58+
UNEXPECTED_TRANSLATION_TYPE = 2302,
59+
// (undocumented)
60+
UNKNOWN_DATE_TYPE_VALUE = 2301,
61+
// (undocumented)
62+
UNKNOWN_ZONE_WIDTH = 2302
5563
}
5664

5765
// (No @packageDocumentation comment for this package)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ export const enum RuntimeErrorCode {
1515
// (undocumented)
1616
HTTP_ORIGIN_MAP_USED_IN_CLIENT = 2803,
1717
// (undocumented)
18+
JSONP_HEADERS_NOT_SUPPORTED = 2812,
19+
// (undocumented)
20+
JSONP_WRONG_METHOD = 2810,
21+
// (undocumented)
22+
JSONP_WRONG_RESPONSE_TYPE = 2811,
23+
// (undocumented)
1824
MISSING_JSONP_MODULE = -2800,
1925
// (undocumented)
2026
NOT_USING_FETCH_BACKEND_IN_SSR = 2801,

packages/common/http/src/errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ export const enum RuntimeErrorCode {
2121
RESPONSE_IS_NOT_A_BLOB = 2807,
2222
RESPONSE_IS_NOT_A_STRING = 2808,
2323
UNHANDLED_OBSERVE_TYPE = 2809,
24+
JSONP_WRONG_METHOD = 2810,
25+
JSONP_WRONG_RESPONSE_TYPE = 2811,
26+
JSONP_HEADERS_NOT_SUPPORTED = 2812,
2427
}

packages/common/http/src/jsonp.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
inject,
1414
Injectable,
1515
runInInjectionContext,
16+
ɵRuntimeError as RuntimeError,
1617
} from '@angular/core';
1718
import {Observable, Observer} from 'rxjs';
1819

@@ -26,6 +27,7 @@ import {
2627
HttpEventType,
2728
HttpResponse,
2829
} from './response';
30+
import {RuntimeErrorCode} from './errors';
2931

3032
// Every request made through JSONP needs a callback name that's unique across the
3133
// whole page. Each request is assigned an id and the callback name is constructed
@@ -115,15 +117,24 @@ export class JsonpClientBackend implements HttpBackend {
115117
// Firstly, check both the method and response type. If either doesn't match
116118
// then the request was improperly routed here and cannot be handled.
117119
if (req.method !== 'JSONP') {
118-
throw new Error(JSONP_ERR_WRONG_METHOD);
120+
throw new RuntimeError(
121+
RuntimeErrorCode.JSONP_WRONG_METHOD,
122+
ngDevMode && JSONP_ERR_WRONG_METHOD,
123+
);
119124
} else if (req.responseType !== 'json') {
120-
throw new Error(JSONP_ERR_WRONG_RESPONSE_TYPE);
125+
throw new RuntimeError(
126+
RuntimeErrorCode.JSONP_WRONG_RESPONSE_TYPE,
127+
ngDevMode && JSONP_ERR_WRONG_RESPONSE_TYPE,
128+
);
121129
}
122130

123131
// Check the request headers. JSONP doesn't support headers and
124132
// cannot set any that were supplied.
125133
if (req.headers.keys().length > 0) {
126-
throw new Error(JSONP_ERR_HEADERS_NOT_SUPPORTED);
134+
throw new RuntimeError(
135+
RuntimeErrorCode.JSONP_HEADERS_NOT_SUPPORTED,
136+
ngDevMode && JSONP_ERR_HEADERS_NOT_SUPPORTED,
137+
);
127138
}
128139

129140
// Everything else happens inside the Observable boundary.
@@ -178,7 +189,7 @@ export class JsonpClientBackend implements HttpBackend {
178189
// if the JSONP script loads successfully. The event itself is unimportant.
179190
// If something went wrong, onLoad() may run without the response callback
180191
// having been invoked.
181-
const onLoad = (event: Event) => {
192+
const onLoad = () => {
182193
// We wrap it in an extra Promise, to ensure the microtask
183194
// is scheduled after the loaded endpoint has executed any potential microtask itself,
184195
// which is not guaranteed in Internet Explorer and EdgeHTML. See issue #39496
@@ -220,7 +231,7 @@ export class JsonpClientBackend implements HttpBackend {
220231
// onError() is the error callback, which runs if the script returned generates
221232
// a Javascript error. It emits the error via the Observable error channel as
222233
// a HttpErrorResponse.
223-
const onError: any = (error: Error) => {
234+
const onError = (error: Error) => {
224235
cleanup();
225236

226237
// Wrap the error in a HttpErrorResponse.

packages/common/http/test/jsonp_spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('JsonpClientBackend', () => {
9090
describe('throws an error', () => {
9191
it('when request method is not JSONP', () =>
9292
expect(() => backend.handle(SAMPLE_REQ.clone<never>({method: 'GET'}))).toThrowError(
93-
JSONP_ERR_WRONG_METHOD,
93+
`NG02810: ${JSONP_ERR_WRONG_METHOD}`,
9494
));
9595
it('when response type is not json', () =>
9696
expect(() =>
@@ -99,15 +99,15 @@ describe('JsonpClientBackend', () => {
9999
responseType: 'text',
100100
}),
101101
),
102-
).toThrowError(JSONP_ERR_WRONG_RESPONSE_TYPE));
102+
).toThrowError(`NG02811: ${JSONP_ERR_WRONG_RESPONSE_TYPE}`));
103103
it('when headers are set in request', () =>
104104
expect(() =>
105105
backend.handle(
106106
SAMPLE_REQ.clone<never>({
107107
headers: new HttpHeaders({'Content-Type': 'application/json'}),
108108
}),
109109
),
110-
).toThrowError(JSONP_ERR_HEADERS_NOT_SUPPORTED));
110+
).toThrowError(`NG02812: ${JSONP_ERR_HEADERS_NOT_SUPPORTED}`));
111111
it('when callback is never called', (done) => {
112112
backend.handle(SAMPLE_REQ).subscribe(undefined, (err: HttpErrorResponse) => {
113113
expect(err.status).toBe(0);

packages/common/src/errors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export const enum RuntimeErrorCode {
2626

2727
// I18n errors
2828
SUSPICIOUS_DATE_FORMAT = 2300,
29+
UNKNOWN_DATE_TYPE_VALUE = 2301,
30+
UNEXPECTED_TRANSLATION_TYPE = 2302,
31+
UNKNOWN_ZONE_WIDTH = 2302,
32+
INVALID_TO_DATE_CONVERSION = 2302,
2933

3034
// Keep 2800 - 2900 for Http Errors.
3135

packages/common/src/i18n/format_date.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
getLocaleNumberSymbol,
2727
getLocaleTimeFormat,
2828
NumberSymbol,
29-
Time,
3029
TranslationWidth,
3130
} from './locale_data_api';
3231
import {RuntimeErrorCode} from '../errors';
@@ -349,7 +348,10 @@ function getDatePart(part: DateType, date: Date): number {
349348
case DateType.Day:
350349
return date.getDay();
351350
default:
352-
throw new Error(`Unknown DateType value "${part}".`);
351+
throw new RuntimeError(
352+
RuntimeErrorCode.UNKNOWN_DATE_TYPE_VALUE,
353+
ngDevMode && `Unknown DateType value "${part}".`,
354+
);
353355
}
354356
}
355357

@@ -435,7 +437,10 @@ function getDateTranslation(
435437
// The `throw new Error` below works around the problem, and the unexpected: never variable
436438
// makes sure tsc still checks this code is unreachable.
437439
const unexpected: never = name;
438-
throw new Error(`unexpected translation type ${unexpected}`);
440+
throw new RuntimeError(
441+
RuntimeErrorCode.UNEXPECTED_TRANSLATION_TYPE,
442+
ngDevMode && `unexpected translation type ${unexpected}`,
443+
);
439444
}
440445
}
441446

@@ -478,7 +483,10 @@ function timeZoneGetter(width: ZoneWidth): DateFormatter {
478483
);
479484
}
480485
default:
481-
throw new Error(`Unknown zone width "${width}"`);
486+
throw new RuntimeError(
487+
RuntimeErrorCode.UNKNOWN_ZONE_WIDTH,
488+
ngDevMode && `Unknown zone width "${width}"`,
489+
);
482490
}
483491
};
484492
}
@@ -938,7 +946,10 @@ export function toDate(value: string | number | Date): Date {
938946

939947
const date = new Date(value as any);
940948
if (!isDate(date)) {
941-
throw new Error(`Unable to convert "${value}" into a date`);
949+
throw new RuntimeError(
950+
RuntimeErrorCode.INVALID_TO_DATE_CONVERSION,
951+
ngDevMode && `Unable to convert "${value}" into a date`,
952+
);
942953
}
943954
return date;
944955
}

0 commit comments

Comments
 (0)