{"record":{"id":"2527ea9ccedaee92","repo":"elastic/elasticsearch","slug":"ex-getmessage","errorCode":null,"errorMessage":"{ex.getMessage()}","messagePattern":"\\{ex\\.getMessage\\(\\)\\}","errorType":"exception","errorClass":"ScriptException","httpStatus":null,"severity":"error","filePath":"modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java","lineNumber":90,"sourceCode":"     * Compile a template string to (in this case) a Mustache object than can\n     * later be re-used for execution to fill in missing parameter values.\n     *\n     * @param templateSource a string representing the template to compile.\n     * @return a compiled template object for later execution.\n     * */\n    @Override\n    public <T> T compile(String templateName, String templateSource, ScriptContext<T> context, Map<String, String> options) {\n        if (context.instanceClazz.equals(TemplateScript.class) == false) {\n            throw new IllegalArgumentException(\"mustache engine does not know how to handle context [\" + context.name + \"]\");\n        }\n        final MustacheFactory factory = createMustacheFactory(options);\n        Reader reader = new StringReader(templateSource);\n        try {\n            Mustache template = factory.compile(reader, \"query-template\");\n            TemplateScript.Factory compiled = params -> new MustacheExecutableScript(template, params);\n            return context.factoryClazz.cast(compiled);\n        } catch (MustacheException ex) {\n            throw new ScriptException(ex.getMessage(), ex, List.of(), templateSource, NAME);\n        }\n\n    }\n\n    @Override\n    public Set<ScriptContext<?>> getSupportedContexts() {\n        return Set.of(TemplateScript.CONTEXT, TemplateScript.INGEST_CONTEXT);\n    }\n\n    private static CustomMustacheFactory createMustacheFactory(Map<String, String> options) {\n        CustomMustacheFactory.Builder builder = CustomMustacheFactory.builder();\n        if (options == null || options.isEmpty()) {\n            return builder.build();\n        }\n\n        if (options.containsKey(Script.CONTENT_TYPE_OPTION)) {\n            builder.mediaType(options.get(Script.CONTENT_TYPE_OPTION));\n        }","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java#L72-L108","documentation":"Thrown by MustacheScriptEngine.compile() when a MustacheException is caught during template compilation. The original MustacheException message is preserved and wrapped in a ScriptException with the template source and engine name ('mustache'). This is the top-level compilation error handler — any syntax error, unsupported feature, or parsing failure in the mustache template surfaces here.","triggerScenarios":"Compiling a search template that contains mustache syntax errors or uses unsupported features. The underlying MustacheException could be from partial/dynamic-partial usage (errors 1246/1247), function parsing failures (1248), or any other mustache parser error. The compile() method catches all MustacheException instances and converts them to ScriptException for consistent error reporting.","commonSituations":"Syntax errors like unclosed tags {{var without }}, mismatched section blocks {{#x}} without {{/x}}, using unsupported partial syntax, or malformed function calls. The actual root cause is in the wrapped exception — read ex.getMessage() to identify the specific compilation failure.","solutions":["Read the inner exception message to identify the specific compilation error (partial unsupported, function parsing, syntax error, etc.)","Validate mustache syntax: ensure all {{ }} tags are properly closed, sections {{#x}}...{{/x}} are balanced","Remove unsupported features: partials ({{>}}), dynamic partials ({{$}}), and any mustache extensions not listed in Elasticsearch docs","Test the template with a standalone mustache validator before sending to Elasticsearch"],"exampleFix":"// before — unclosed section tag:\n{\"query\": {\"bool\": {\"must\": {{#filters}}{\"term\": {\"x\": \"{{.}}\"}}{{/filters}} ]}}\n\n// after — properly balanced and valid:\n{\"query\": {\"bool\": {\"must\": [{{#filters}}{\"term\": {\"x\": \"{{.}}\"}}{{/filters}}]}}}","handlingStrategy":"try-catch","validationCode":"// Validate template syntax before sending to Elasticsearch\n// Use a local mustache parser to check syntax, or use a regex-based pre-check\n// for common errors: unclosed tags, unbalanced sections\nboolean isBalanced(String src) {\n    int depth = 0;\n    Pattern p = Pattern.compile(\"\\\\{\\{(#|/)([^}]+)\\}\\}\");\n    Matcher m = p.matcher(src);\n    while (m.find()) {\n        depth += m.group(1).equals(\"#\") ? 1 : -1;\n    }\n    return depth == 0;\n}","typeGuard":null,"tryCatchPattern":"// Catch ScriptException when compiling templates\ntry {\n    engine.compile(templateName, templateSource, context, options);\n} catch (ScriptException e) {\n    // e.getMessage() contains the original mustache error\n    throw new IllegalArgumentException(\"Template compilation failed: \" + e.getMessage(), e);\n}","preventionTips":["Validate mustache syntax before deploying templates","Ensure all {{ }} tags are closed and sections are balanced","Avoid unsupported features: partials, dynamic partials","Store and test templates in a dev environment before production use"],"tags":["elasticsearch","lang-mustache","compile","script-exception","template"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}