Improve error handling

This commit is contained in:
tdurieux
2026-05-06 17:03:19 +03:00
parent 48e782946a
commit 804bbffb7a
3 changed files with 66 additions and 9 deletions
+11 -5
View File
@@ -292,11 +292,17 @@ function compileTerms(terms: string[] | undefined): CompiledTermVariant[] {
unicode: variant.unicode,
});
const baseFlags = variant.unicode ? "iu" : "i";
compiled.push({
replaceRegex: new RegExp(bounded, "g" + baseFlags),
testRegex: new RegExp(bounded, baseFlags),
mask,
});
// A user-supplied regex can be valid without `u` but illegal with it
// (e.g. `[\w-\.]` — a range between class shorthands is rejected only
// in unicode mode). Skip variants that fail to compile so the other
// variant still anonymizes.
try {
const replaceRegex = new RegExp(bounded, "g" + baseFlags);
const testRegex = new RegExp(bounded, baseFlags);
compiled.push({ replaceRegex, testRegex, mask });
} catch {
continue;
}
}
}
return compiled;