{"record":{"id":"95bce731372ff69b","repo":"elastic/elasticsearch","slug":"exception-while-parsing-mustache-function-at-line","errorCode":null,"errorMessage":"Exception while parsing mustache function at line {tc.line()}","messagePattern":"Exception while parsing mustache function at line (.+?)","errorType":"exception","errorClass":"MustacheException","httpStatus":null,"severity":"error","filePath":"modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java","lineNumber":343,"sourceCode":"\n        UrlEncoderCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String variable) {\n            super(tc, df, mustache.getCodes(), variable);\n            this.encoder = new UrlEncoder();\n        }\n\n        @Override\n        public Writer run(Writer writer, List<Object> scopes) {\n            if (getCodes() != null) {\n                for (Code code : getCodes()) {\n                    try (StringWriter capture = new StringWriter()) {\n                        code.execute(capture, scopes);\n\n                        String s = capture.toString();\n                        if (s != null) {\n                            encoder.encode(s, writer);\n                        }\n                    } catch (IOException e) {\n                        throw new MustacheException(\"Exception while parsing mustache function at line \" + tc.line(), e);\n                    }\n                }\n            }\n            return writer;\n        }\n\n        static boolean match(String variable) {\n            return CODE.equalsIgnoreCase(variable);\n        }\n    }\n\n    @FunctionalInterface\n    interface Encoder {\n        /**\n         * Encodes the {@code s} string and writes it to the {@code writer} {@link Writer}.\n         *\n         * @param s      The string to encode\n         * @param writer The {@link Writer} to which the encoded string will be written to","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java#L325-L361","documentation":"Thrown by UrlEncoderCode.run() when an IOException occurs during the execution of the {{#url}}...{{/url}} function. The function captures each inner code's output to a StringWriter and URL-encodes it; if the capture or encoding step fails with IOException, it is wrapped in this MustacheException with the template line number.","triggerScenarios":"Using the {{#url}}variable{{/url}} function in a search template where the inner code execution throws IOException during output capture. Since StringWriter.write() does not throw IOException, this typically indicates a failure in the inner Code's execute() method or an edge case in the encoding logic.","commonSituations":"Rarely encountered. Could occur if the inner code represents a complex nested structure that fails during execution. More likely seen alongside other rendering errors in deeply nested templates. The line number in the message helps pinpoint the template location.","solutions":["Simplify the content inside {{#url}}...{{/url}} to a single variable: {{#url}}search_term{{/url}}","Check the template line number in the error message to locate the failing section","Verify the parameter value being URL-encoded is a simple string","If the error persists, URL-encode the value client-side before passing it as a parameter"],"exampleFix":"// before — complex content inside url function:\n{{#url}}{{#toJson}}filters{{/toJson}}{{/url}}\n\n// after — simple variable:\n{{#url}}query{{/url}}","handlingStrategy":"validation","validationCode":"// Validate that {{#url}} blocks contain simple variable references only\n// Check template source for {{#url}} blocks with complex nested content\n// and simplify to {{#url}}singleVar{{/url}}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep {{#url}} blocks simple: {{#url}}variable{{/url}}","URL-encode values client-side as an alternative","Avoid nesting other functions inside {{#url}}"],"tags":["elasticsearch","lang-mustache","url-encode","template","io"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}