{"record":{"id":"f6ac5d770e82ed28","repo":"karatelabs/karate","slug":"configure-logging-mask-patterns-invalid-regex-skipped","errorCode":null,"errorMessage":"configure logging.mask.patterns[{}] invalid regex '{}': {} — skipped","messagePattern":"configure logging\\.mask\\.patterns\\[(.+?)\\] invalid regex '(.+?)': (.+?) — skipped","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"karate-core/src/main/java/io/karatelabs/output/LogMask.java","lineNumber":126,"sourceCode":"        if (map.get(\"patterns\") instanceof List<?> list) {\n            int idx = 0;\n            for (Object item : list) {\n                if (item instanceof Map<?, ?> pm) {\n                    Object regex = pm.get(\"regex\");\n                    if (regex == null) {\n                        logger.warn(\"configure logging.mask.patterns[{}] missing 'regex' — skipped\", idx);\n                        idx++;\n                        continue;\n                    }\n                    Object rep = pm.get(\"replacement\");\n                    String repStr = rep == null ? replacement : rep.toString();\n                    try {\n                        patternRules.add(new PatternRule(Pattern.compile(regex.toString()), repStr));\n                    } catch (PatternSyntaxException e) {\n                        // Skip rather than fail-fast — a typo in one rule shouldn't blow up\n                        // the whole config-js eval. The warn names the entry so the user\n                        // can find it. Other rules in the same mask still apply.\n                        logger.warn(\"configure logging.mask.patterns[{}] invalid regex '{}': {} — skipped\",\n                                idx, regex, e.getDescription());\n                    }\n                }\n                idx++;\n            }\n        }\n        JavaCallable enableForUri = map.get(\"enableForUri\") instanceof JavaCallable c ? c : null;\n        if (headers.isEmpty() && jsonPaths.isEmpty() && patternRules.isEmpty()) {\n            // User provided a mask map but every rule list is empty / invalid. Without this\n            // warn the mask silently does nothing, which is hard to debug.\n            logger.warn(\"configure logging.mask: no usable rules — set at least one of \"\n                    + \"headers / jsonPaths / patterns. mask is OFF.\");\n            return null;\n        }\n        return new LogMask(headers, jsonPaths, patternRules, replacement, enableForUri);\n    }\n\n    /**","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/output/LogMask.java#L108-L144","documentation":"A `patterns` entry in a logging.mask config provided a `regex` string that `Pattern.compile` could not parse. The library skips that single rule and logs this warning naming the index, the bad regex, and the syntax error description, so the rest of the mask keeps working.","triggerScenarios":"`karate.configure('logging.mask', { patterns: [{ regex: '[a-z+', replacement: 'x' }] })` — any syntactically invalid Java regex (unclosed bracket/paren, bad quantifier, dangling escape) in a patterns entry.","commonSituations":"Hand-written regex typos; regexes copied from JavaScript/PCRE with constructs Java's `Pattern` rejects (e.g. lookbehind of variable length, `\\p{...}` script names); dynamically generated regexes from untrusted string concatenation.","solutions":["Fix the regex syntax; the warning includes `e.getDescription()` pointing at the offending part","Paste the regex into a Java-regex-aware tester (java.util.Pattern syntax, not JS)","Escape special characters properly, especially backslashes","Split complex patterns into simpler ones to isolate the syntax error"],"exampleFix":"// before\nkarate.configure('logging.mask', { patterns: [{ regex: '[0-9{16}', replacement: '***' }] })\n// after\nkarate.configure('logging.mask', { patterns: [{ regex: '[0-9]{16}', replacement: '***' }] })","handlingStrategy":"validation","validationCode":"for (var p : patterns) {\n    try { java.util.regex.Pattern.compile((String) p.get(\"regex\")); }\n    catch (java.util.regex.PatternSyntaxException e) {\n        throw new IllegalArgumentException(\"bad mask regex: \" + e.getDescription());\n    }\n}","typeGuard":null,"tryCatchPattern":"try { Pattern.compile(regexStr); } catch (PatternSyntaxException e) { /* fix regex: \" + e.getDescription() */ }","preventionTips":["Test regexes with Java Pattern syntax, not JavaScript regex","Escape backslashes correctly in string literals","Compile patterns once in a unit test to catch typos early"],"tags":["regex","logging","configuration","masking"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}