{"record":{"id":"2f46aac298e423c7","repo":"elastic/elasticsearch","slug":"unexpected-end-of-file","errorCode":null,"errorMessage":"Unexpected end of file","messagePattern":"Unexpected end of file","errorType":"exception","errorClass":"XContentEOFException","httpStatus":null,"severity":"error","filePath":"libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/JsonXContentParser.java","lineNumber":76,"sourceCode":"        JsonLocation loc = e.getLocation();\n        if (loc != null) {\n            return new XContentLocation(loc.getLineNr(), loc.getColumnNr(), loc.getByteOffset());\n        } else {\n            return null;\n        }\n    }\n\n    private static XContentParseException newXContentParseException(JsonProcessingException e) {\n        return new XContentParseException(getLocation(e), e.getMessage(), e);\n    }\n\n    /**\n     * Handle parser exception depending on type.\n     * This converts known exceptions to XContentParseException and rethrows them.\n     */\n    static IOException handleParserException(IOException e) throws IOException {\n        switch (e) {\n            case JsonEOFException eof -> throw new XContentEOFException(getLocation(eof), \"Unexpected end of file\", e);\n            case JsonParseException pe -> throw newXContentParseException(pe);\n            case InputCoercionException ice -> throw newXContentParseException(ice);\n            case CharConversionException cce -> throw new XContentParseException(null, cce.getMessage(), cce);\n            case StreamConstraintsException sce -> throw newXContentParseException(sce);\n            default -> {\n                return e;\n            }\n        }\n    }\n\n    @Override\n    public Token nextToken() throws IOException {\n        try {\n            return convertToken(parser.nextToken());\n        } catch (IOException e) {\n            throw handleParserException(e);\n        }\n    }","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/JsonXContentParser.java#L58-L94","documentation":"handleParserException maps Jackson's JsonEOFException to XContentEOFException with the message \"Unexpected end of file\". This fires when the parser hit physical end of input while still inside a value, object, or array - i.e. the document was truncated mid-stream.","triggerScenarios":"Calling nextToken()/nextFieldName()/getText() etc. on a JsonXContentParser whose underlying stream ended before the JSON structure was complete.","commonSituations":"Truncated HTTP response body; network read cut short; a stream that was closed early; log/event payload missing the closing brace; chunked transfer that dropped the final chunk; feeding a parser from a byte array that was clipped.","solutions":["Ensure the source stream delivers the complete document before parsing (read fully or check Content-Length)","Add framing / length checks upstream of the parser","Retry the read if the source is a flaky network stream","Validate length before handing bytes to the parser"],"exampleFix":"// before: read may be partial\ntry (XContentParser p = XContentType.JSON.xContent().createParser(config, in)) {\n    p.nextToken();\n}\n// after: buffer fully first\nbyte[] bytes = in.readAllBytes();\ntry (XContentParser p = XContentType.JSON.xContent().createParser(config, new ByteArrayInputStream(bytes))) {\n    p.nextToken();\n}","handlingStrategy":"try-catch","validationCode":"byte[] bytes = in.readAllBytes();\nif (bytes.length == 0) throw new EOFException(\"empty input\");\ntry (XContentParser p = XContentType.JSON.xContent().createParser(config, new ByteArrayInputStream(bytes))) {\n    p.nextToken();\n}","typeGuard":"static boolean isCompleteJson(byte[] b) {\n    try (var p = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, new ByteArrayInputStream(b))) {\n        while (p.nextToken() != null) {}\n        return true;\n    } catch (IOException e) { return false; }\n}","tryCatchPattern":"try { parser.nextToken(); }\ncatch (XContentEOFException e) { /* truncated upstream - refetch */ }","preventionTips":["Buffer the full body before parsing when possible","Validate Content-Length against received bytes","Retry truncated network reads once"],"tags":["x-content","json","parsing","io"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}