Skip to content
This repository was archived by the owner on May 4, 2020. It is now read-only.

Commit 58dd475

Browse files
author
Long Ho
committed
feat(intl-messageformat): add more debug info to errors
1 parent 435ad8c commit 58dd475

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

packages/intl-messageformat/src/error.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,47 @@ export const enum ErrorCode {
99

1010
export class FormatError extends Error {
1111
public readonly code: ErrorCode;
12-
constructor(msg: string, code: ErrorCode) {
12+
/**
13+
* Original message we're trying to format
14+
* `undefined` if we're only dealing w/ AST
15+
*
16+
* @type {(string | undefined)}
17+
* @memberof FormatError
18+
*/
19+
public readonly originalMessage: string | undefined;
20+
constructor(msg: string, code: ErrorCode, originalMessage?: string) {
1321
super(msg);
1422
this.code = code;
23+
this.originalMessage = originalMessage;
1524
}
1625
public toString() {
1726
return `[formatjs Error: ${this.code}] ${this.message}`;
1827
}
1928
}
2029

2130
export class InvalidValueError extends FormatError {
22-
constructor(variableId: string, value: any, options: string[]) {
31+
constructor(
32+
variableId: string,
33+
value: any,
34+
options: string[],
35+
originalMessage?: string
36+
) {
2337
super(
2438
`Invalid values for "${variableId}": "${value}". Options are "${Object.keys(
2539
options
2640
).join('", "')}"`,
27-
ErrorCode.INVALID_VALUE
41+
ErrorCode.INVALID_VALUE,
42+
originalMessage
43+
);
44+
}
45+
}
46+
47+
export class InvalidValueTypeError extends FormatError {
48+
constructor(value: any, type: string, originalMessage?: string) {
49+
super(
50+
`Value for "${value}" must be of type ${type}`,
51+
ErrorCode.INVALID_VALUE,
52+
originalMessage
2853
);
2954
}
3055
}
@@ -33,7 +58,8 @@ export class MissingValueError extends FormatError {
3358
constructor(variableId: string, originalMessage?: string) {
3459
super(
3560
`The intl string context variable "${variableId}" was not provided to the string "${originalMessage}"`,
36-
ErrorCode.MISSING_VALUE
61+
ErrorCode.MISSING_VALUE,
62+
originalMessage
3763
);
3864
}
3965
}

packages/intl-messageformat/src/formatters.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export function formatToParts<T>(
203203
const {children, value} = el;
204204
const formatFn = values[value];
205205
if (!isFormatXMLElementFn<T>(formatFn)) {
206-
throw new TypeError(`Value for "${value}" must be a function`);
206+
throw new InvalidValueTypeError(value, 'function', originalMessage);
207207
}
208208
const parts = formatToParts<T>(
209209
children,
@@ -231,7 +231,12 @@ export function formatToParts<T>(
231231
if (isSelectElement(el)) {
232232
const opt = el.options[value as string] || el.options.other;
233233
if (!opt) {
234-
throw new InvalidValueError(el.value, value, Object.keys(el.options));
234+
throw new InvalidValueError(
235+
el.value,
236+
value,
237+
Object.keys(el.options),
238+
originalMessage
239+
);
235240
}
236241
result.push(
237242
...formatToParts(opt.value, locales, formatters, formats, values)
@@ -246,7 +251,8 @@ export function formatToParts<T>(
246251
`Intl.PluralRules is not available in this environment.
247252
Try polyfilling it using "@formatjs/intl-pluralrules"
248253
`,
249-
ErrorCode.MISSING_INTL_API
254+
ErrorCode.MISSING_INTL_API,
255+
originalMessage
250256
);
251257
}
252258
const rule = formatters
@@ -255,7 +261,12 @@ Try polyfilling it using "@formatjs/intl-pluralrules"
255261
opt = el.options[rule] || el.options.other;
256262
}
257263
if (!opt) {
258-
throw new InvalidValueError(el.value, value, Object.keys(el.options));
264+
throw new InvalidValueError(
265+
el.value,
266+
value,
267+
Object.keys(el.options),
268+
originalMessage
269+
);
259270
}
260271
result.push(
261272
...formatToParts(

0 commit comments

Comments
 (0)