@@ -14,6 +14,12 @@ import {
14
14
parseDateTimeSkeleton ,
15
15
isTagElement ,
16
16
} from 'intl-messageformat-parser' ;
17
+ import {
18
+ MissingValueError ,
19
+ InvalidValueError ,
20
+ ErrorCode ,
21
+ FormatError ,
22
+ } from './error' ;
17
23
18
24
export interface Formats {
19
25
number : Record < string , Intl . NumberFormatOptions > ;
@@ -58,14 +64,6 @@ export type MessageFormatPart<T> = LiteralPart | ObjectPart<T>;
58
64
59
65
export type PrimitiveType = string | number | boolean | null | undefined | Date ;
60
66
61
- class FormatError extends Error {
62
- public readonly variableId ?: string ;
63
- constructor ( msg ?: string , variableId ?: string ) {
64
- super ( msg ) ;
65
- this . variableId = variableId ;
66
- }
67
- }
68
-
69
67
function mergeLiteral < T > (
70
68
parts : MessageFormatPart < T > [ ]
71
69
) : MessageFormatPart < T > [ ] {
@@ -139,9 +137,7 @@ export function formatToParts<T>(
139
137
140
138
// Enforce that all required values are provided by the caller.
141
139
if ( ! ( values && varName in values ) ) {
142
- throw new FormatError (
143
- `The intl string context variable "${ varName } " was not provided to the string "${ originalMessage } "`
144
- ) ;
140
+ throw new MissingValueError ( varName , originalMessage ) ;
145
141
}
146
142
147
143
let value = values [ varName ] ;
@@ -235,11 +231,7 @@ export function formatToParts<T>(
235
231
if ( isSelectElement ( el ) ) {
236
232
const opt = el . options [ value as string ] || el . options . other ;
237
233
if ( ! opt ) {
238
- throw new RangeError (
239
- `Invalid values for "${
240
- el . value
241
- } ": "${ value } ". Options are "${ Object . keys ( el . options ) . join ( '", "' ) } "`
242
- ) ;
234
+ throw new InvalidValueError ( el . value , value , Object . keys ( el . options ) ) ;
243
235
}
244
236
result . push (
245
237
...formatToParts ( opt . value , locales , formatters , formats , values )
@@ -250,21 +242,20 @@ export function formatToParts<T>(
250
242
let opt = el . options [ `=${ value } ` ] ;
251
243
if ( ! opt ) {
252
244
if ( ! Intl . PluralRules ) {
253
- throw new FormatError ( `Intl.PluralRules is not available in this environment.
245
+ throw new FormatError (
246
+ `Intl.PluralRules is not available in this environment.
254
247
Try polyfilling it using "@formatjs/intl-pluralrules"
255
- ` ) ;
248
+ ` ,
249
+ ErrorCode . MISSING_INTL_API
250
+ ) ;
256
251
}
257
252
const rule = formatters
258
253
. getPluralRules ( locales , { type : el . pluralType } )
259
254
. select ( ( value as number ) - ( el . offset || 0 ) ) ;
260
255
opt = el . options [ rule ] || el . options . other ;
261
256
}
262
257
if ( ! opt ) {
263
- throw new RangeError (
264
- `Invalid values for "${
265
- el . value
266
- } ": "${ value } ". Options are "${ Object . keys ( el . options ) . join ( '", "' ) } "`
267
- ) ;
258
+ throw new InvalidValueError ( el . value , value , Object . keys ( el . options ) ) ;
268
259
}
269
260
result . push (
270
261
...formatToParts (
0 commit comments