{"record":{"id":"41d9d610a560c436","repo":"elastic/elasticsearch","slug":"error-running-template","errorCode":null,"errorMessage":"Error running {template}","messagePattern":"Error running (.+?)","errorType":"exception","errorClass":"GeneralScriptException","httpStatus":null,"severity":"error","filePath":"modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java","lineNumber":158,"sourceCode":"            this.template = template;\n            this.params = params;\n        }\n\n        @Override\n        public String execute() {\n            StringWriter writer = new SizeLimitingStringWriter(sizeLimit);\n            try {\n                template.execute(writer, params);\n            } catch (Exception e) {\n                // size limit exception can appear at several places in the causal list depending on script & context\n                if (ExceptionsHelper.unwrap(e, SizeLimitingStringWriter.SizeLimitExceededException.class) != null) {\n                    // don't log, client problem\n                    throw new ElasticsearchParseException(\"Mustache script result size limit exceeded\", e);\n                }\n                if (shouldLogException(e)) {\n                    logger.error(() -> format(\"Error running %s\", template), e);\n                }\n                throw new GeneralScriptException(\"Error running \" + template, e);\n            }\n            return writer.toString();\n        }\n\n        public boolean shouldLogException(Throwable e) {\n            return e.getCause() != null && e.getCause() instanceof MustacheInvalidParameterException == false;\n        }\n    }\n\n}\n","sourceCodeStart":140,"sourceCodeEnd":169,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java#L140-L169","documentation":"Thrown by MustacheExecutableScript.execute() as a catch-all for any exception during template rendering that is not a size-limit violation and not a missing-parameter error. The original exception is wrapped in a GeneralScriptException with the template's toString() representation. If shouldLogException() returns true (cause exists and is not MustacheInvalidParameterException), the error is also logged at ERROR level before throwing.","triggerScenarios":"Any runtime failure during template.execute() that doesn't match the size-limit or missing-param patterns. This includes encoding failures (error 1244), JSON conversion failures (1250), URL encoding failures (1252), null pointer exceptions during rendering, or any unexpected mustache library internal error. The template variable in the message is the Mustache object's toString().","commonSituations":"Passing null or incompatible parameter types that cause failures during rendering. Template logic errors like accessing nested properties on null values. Underlying encoder or serializer failures. The root cause is in the wrapped exception — check server logs for the full stack trace.","solutions":["Check the server logs for the full stack trace — the ERROR log entry includes the underlying cause","Validate all parameter types match what the template expects (strings, numbers, arrays, maps)","Test the template with minimal parameters first, then add complexity incrementally to isolate the failing input","If the cause is a MustacheException from encoding, see the specific encoding error documentation"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Validate parameter types before template execution\nvoid validateTemplateParams(Map<String, Object> params, Set<String> required) {\n    for (String key : required) {\n        if (!params.containsKey(key)) {\n            throw new IllegalArgumentException(\"Missing required param: \" + key);\n        }\n    }\n    // check for null values that could cause NPEs during rendering\n    params.forEach((k, v) -> {\n        if (v == null) log.warn(\"Param [{}] is null — may cause rendering error\", k);\n    });\n}","typeGuard":null,"tryCatchPattern":"// Catch GeneralScriptException at the call site\ntry {\n    String result = script.execute();\n} catch (GeneralScriptException e) {\n    // check server logs for full stack trace logged at ERROR level\n    log.error(\"Template execution failed: {}\", e.getMessage(), e);\n    // inspect e.getCause() for the root failure\n}","preventionTips":["Validate all parameter types match template expectations before execution","Avoid null parameter values unless the template handles them","Check server ERROR logs for the full stack trace of the underlying cause","Test templates with minimal parameters first, then add complexity incrementally"],"tags":["elasticsearch","lang-mustache","runtime","template","general-error"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}