Skip to content

Commit df11ca8

Browse files
fix(mf2): Catch errors thrown during MatchSelectorKeys (unicode-org/message-format-wg#828)
1 parent 80e1373 commit df11ca8

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

packages/mf2-messageformat/src/errors.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ export class MessageResolutionError extends MessageError {
109109
*/
110110
export class MessageSelectionError extends MessageError {
111111
declare type: 'bad-selector' | 'no-match';
112-
constructor(type: typeof MessageSelectionError.prototype.type) {
112+
cause?: unknown;
113+
constructor(
114+
type: typeof MessageSelectionError.prototype.type,
115+
cause?: unknown
116+
) {
113117
super(type, `Selection error: ${type}`);
118+
if (cause !== undefined) this.cause = cause;
114119
}
115120
}

packages/mf2-messageformat/src/select-pattern.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ export function selectPattern(context: Context, message: Message): Pattern {
3636
if (key.type !== '*') sc.keys.add(key.value);
3737
}
3838
}
39-
sc.best = sc.keys.size ? sc.selectKey(sc.keys) : null;
39+
try {
40+
sc.best = sc.keys.size ? sc.selectKey(sc.keys) : null;
41+
} catch (error) {
42+
context.onError(new MessageSelectionError('bad-selector', error));
43+
sc.selectKey = () => null;
44+
sc.best = null;
45+
}
4046

4147
// Leave out all candidate variants that aren't the best,
4248
// or only the catchall ones, if nothing else matches.

0 commit comments

Comments
 (0)