{"record":{"id":"fd3ff590d5853934","repo":"elastic/elasticsearch","slug":"unable-to-encode-value","errorCode":null,"errorMessage":"Unable to encode value","messagePattern":"Unable to encode value","errorType":"exception","errorClass":"MustacheException","httpStatus":null,"severity":"error","filePath":"modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java","lineNumber":99,"sourceCode":"     * @deprecated Use {@link #builder()} instead to retrieve a {@link Builder} object that can be used to create a factory.\n     */\n    @Deprecated\n    public CustomMustacheFactory() {\n        this(DEFAULT_MEDIA_TYPE, DEFAULT_DETECT_MISSING_PARAMS);\n    }\n\n    private CustomMustacheFactory(String mediaType, boolean detectMissingParams) {\n        super(resourceName -> null); // we do not resolve templates via files or the classpath, etc.\n        setObjectHandler(new CustomReflectionObjectHandler(detectMissingParams));\n        this.encoder = createEncoder(mediaType);\n    }\n\n    @Override\n    public void encode(String value, Writer writer) {\n        try {\n            encoder.encode(value, writer);\n        } catch (IOException e) {\n            throw new MustacheException(\"Unable to encode value\", e);\n        }\n    }\n\n    static Encoder createEncoder(String mediaType) {\n        final Supplier<Encoder> supplier = ENCODERS.get(mediaType);\n        if (supplier == null) {\n            throw new IllegalArgumentException(\"No encoder found for media type [\" + mediaType + \"]\");\n        }\n        return supplier.get();\n    }\n\n    @Override\n    public MustacheVisitor createMustacheVisitor() {\n        return new CustomMustacheVisitor(this);\n    }\n\n    public static Builder builder() {\n        return new Builder();","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java#L81-L117","documentation":"Thrown by CustomMustacheFactory.encode() when the configured encoder fails to write an encoded value due to an IOException. This is a low-level wrapper that catches I/O failures from the underlying Writer during template rendering and converts them into a MustacheException. The encoder itself (JsonEscapeEncoder, UrlEncoder, or DefaultEncoder) does not typically throw, so the IOException usually originates from the downstream Writer being closed or the output stream failing.","triggerScenarios":"Executing a search template (via _search/template or _msearch/template) where template rendering writes to a Writer that encounters an I/O error mid-encode. This can happen when the output buffer is closed prematurely, when the SizeLimitingStringWriter has already thrown, or under memory pressure causing stream failures.","commonSituations":"Template execution under memory pressure or after a size-limit violation has partially corrupted the writer state. Concurrent use of a shared factory in a way that stresses I/O paths. Rarely seen in normal operation — usually a secondary symptom of another failing condition.","solutions":["Check for size-limit issues first — if mustache.max_output_size_bytes is being hit, the root cause is the size limit, not encoding","Simplify the template to reduce output volume and retry","Check server logs for concurrent OOM or stream-closed errors that could explain the I/O failure","If reproducible, isolate the template parameter values that trigger the error and test them individually"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// When executing templates programmatically, catch MustacheException\ntry {\n    TemplateScript.Factory factory = engine.compile(...);\n    String result = factory.newInstance(params).execute();\n} catch (MustacheException e) {\n    // check cause for IOException — often a secondary symptom\n    log.warn(\"Template encoding failed: {}\", e.getMessage());\n}","preventionTips":["Monitor for size-limit violations which can cascade into encoding errors","Ensure templates don't produce excessively large output","Check server memory if encoding errors appear frequently"],"tags":["elasticsearch","lang-mustache","encoding","io","template"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}