{"record":{"id":"0403cd8889c2fe77","repo":"elastic/elasticsearch","slug":"name-failed-to-parse-object","errorCode":null,"errorMessage":"[{name}] failed to parse object","messagePattern":"\\[(.+?)\\] failed to parse object","errorType":"exception","errorClass":"XContentParseException","httpStatus":null,"severity":"error","filePath":"libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java","lineNumber":161,"sourceCode":"     *        argument is the value of the context provided to the {@link #parse(XContentParser, Object) parse function}. If any of the\n     *        constructor arguments aren't defined in the XContent then parsing will throw an error. We use an array here rather than a\n     *        {@code Map<String, Object>} to save on allocations.\n     */\n    public ConstructingObjectParser(String name, boolean ignoreUnknownFields, BiFunction<Object[], Context, Value> builder) {\n        objectParser = new ObjectParser<>(name, ignoreUnknownFields, null);\n        this.builder = builder;\n\n    }\n\n    /**\n     * Call this to do the actual parsing. This implements {@link BiFunction} for conveniently integrating with ObjectParser.\n     */\n    @Override\n    public Value apply(XContentParser parser, Context context) {\n        try {\n            return parse(parser, context);\n        } catch (IOException e) {\n            throw new XContentParseException(parser.getTokenLocation(), \"[\" + objectParser.getName() + \"] failed to parse object\", e);\n        }\n    }\n\n    @Override\n    public Value parse(XContentParser parser, Context context) throws IOException {\n        return objectParser.parse(parser, new Target(parser, context), context).finish();\n    }\n\n    /**\n     * Pass the {@linkplain BiConsumer} this returns the declare methods to declare a required constructor argument. See this class's\n     * javadoc for an example. The order in which these are declared matters: it is the order that they come in the array passed to\n     * {@link #builder} and the order that missing arguments are reported to the user if any are missing. When all of these parameters are\n     * parsed from the {@linkplain XContentParser} the target object is immediately built.\n     */\n    @SuppressWarnings(\"unchecked\") // Safe because we never call the method. This is just trickery to make the interface pretty.\n    public static <Value, FieldT> BiConsumer<Value, FieldT> constructorArg() {\n        return (BiConsumer<Value, FieldT>) REQUIRED_CONSTRUCTOR_ARG_MARKER;\n    }","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java#L143-L179","documentation":"ConstructingObjectParser.apply wraps any IOException raised during parse() into an XContentParseException tagged with the parser's name and the token location. apply() is the BiFunction entry point used when integrating with ObjectParser pipelines. The root cause is preserved as the exception's cause, so the inner IOException (often a low-level read or sub-parse failure) is what actually went wrong.","triggerScenarios":"Any IOException during parsing the object body: malformed token stream, premature EOF, a declared field's sub-parser failing, or a NamedObject lookup throwing. Calling apply(parser, context) on truncated or structurally invalid input.","commonSituations":"Truncated request bodies in REST handlers. Partial reads from a stream that closes mid-object. Chained parsers where an inner ObjectParser throws and the outer ConstructingObjectParser propagates it through apply().","solutions":["Read the cause of the thrown XContentParseException; the actionable detail (which field/token/IO problem) is there, not in the wrapper message.","Validate the input is complete and well-formed before handing it to apply(), or ensure the upstream reader cannot close mid-object.","If integrating via parse() directly is possible, call it to receive the raw IOException instead of the wrapped form, simplifying diagnostics."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    Value v = parser.apply(parser, ctx);\n} catch (XContentParseException e) {\n    Throwable root = e.getCause(); // the real IOException\n    // handle/recover based on root\n}","preventionTips":["Prefer parse() over apply() when you can handle IOException directly, to avoid the wrapper.","Validate input completeness (Content-Length, non-null stream) before parsing.","Log the cause, not just the wrapper message, to preserve diagnostics."],"tags":["xcontent","constructing-object-parser","deserialization","wrapper"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}