{"record":{"id":"d64c5aebd7f7fa75","repo":"elastic/elasticsearch","slug":"exception-while-parsing-mustache-function-at","errorCode":null,"errorMessage":"Exception while parsing mustache function [{}] at line {}","messagePattern":"Exception while parsing mustache function \\[(.+?)\\] at line (.+?)","errorType":"validation","errorClass":"MustacheException","httpStatus":null,"severity":"error","filePath":"modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java","lineNumber":205,"sourceCode":"         * At compile time, this function extracts the name of the variable:\n         * {{#toJson}}variable_name{{/toJson}}\n         */\n        protected static String extractVariableName(String fn, Mustache mustache, TemplateContext tc) {\n            Code[] codes = mustache.getCodes();\n            if (codes == null || codes.length != 1) {\n                throw new MustacheException(\"Mustache function [\" + fn + \"] must contain one and only one identifier\");\n            }\n\n            try (StringWriter capture = new StringWriter()) {\n                // Variable name is in plain text and has type WriteCode\n                if (codes[0] instanceof WriteCode) {\n                    codes[0].execute(capture, List.of());\n                } else {\n                    codes[0].identity(capture);\n                }\n                return capture.toString();\n            } catch (IOException e) {\n                throw new MustacheException(\"Exception while parsing mustache function [\" + fn + \"] at line \" + tc.line(), e);\n            }\n        }\n    }\n\n    /**\n     * This function renders {@link Iterable} and {@link Map} as their JSON representation\n     */\n    private static class ToJsonCode extends CustomCode {\n\n        private static final String CODE = \"toJson\";\n\n        ToJsonCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String variable) {\n            super(tc, df, mustache, CODE);\n            if (CODE.equalsIgnoreCase(variable) == false) {\n                throw new MustacheException(\"Mismatch function code [\" + CODE + \"] cannot be applied to [\" + variable + \"]\");\n            }\n        }\n","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java#L187-L223","documentation":"Thrown by CustomCode.extractVariableName() when an IOException occurs while capturing the variable name from a custom mustache function block at compile time. This wraps the I/O failure (from writing to a StringWriter during code identity/capture) in a MustacheException. Since the capture uses an in-memory StringWriter, an actual IOException here is extremely rare and indicates an internal failure.","triggerScenarios":"An internal I/O failure during the compile-time extraction of a variable name from a toJson, join, or url function block. The StringWriter used for capture should not throw IOException under normal circumstances; this path is a defensive catch for unexpected internal errors.","commonSituations":"Virtually never seen in production. Could theoretically occur under extreme memory pressure or if a mustache Code implementation's execute/identity method throws an unexpected IOException. More likely indicates a bug in a custom mustache extension or an edge case in the mustache library itself.","solutions":["Simplify the function block to a bare variable name and retry","Check for memory pressure or OOM conditions on the node","Report as a bug if reproducible with a minimal template — include the full template source and the line number from the error","As a workaround, avoid the custom function and inline the value directly"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Catch ScriptException (wrapping MustacheException) at compile time\ntry {\n    engine.compile(name, source, context, options);\n} catch (ScriptException e) {\n    // check for internal I/O failure during parsing\n    log.error(\"Template compilation failed: {}\", e.getMessage(), e);\n}","preventionTips":["Keep custom function blocks simple (single bare variable name)","Monitor node memory if internal I/O errors appear","Report reproducible cases as bugs"],"tags":["elasticsearch","lang-mustache","compile","io","internal"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}