{"record":{"id":"352ba9978e51edf3","repo":"elastic/elasticsearch","slug":"failed-to-build-name-after-last-required-field-352ba9","errorCode":null,"errorMessage":"Failed to build [{name}] after last required field arrived","messagePattern":"Failed to build \\[(.+?)\\] after last required field arrived","errorType":"exception","errorClass":"XContentParseException","httpStatus":null,"severity":"error","filePath":"libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java","lineNumber":591,"sourceCode":"\n        private void buildTarget() {\n            try {\n                targetObject = builder.apply(constructorArgs, context);\n                if (queuedOrderedModeCallback != null) {\n                    queuedOrderedModeCallback.accept(targetObject);\n                }\n                while (queuedFieldsCount > 0) {\n                    queuedFieldsCount -= 1;\n                    queuedFields[queuedFieldsCount].accept(targetObject);\n                }\n            } catch (XContentParseException e) {\n                throw new XContentParseException(\n                    e.getLocation(),\n                    \"failed to build [\" + objectParser.getName() + \"] after last required field arrived\",\n                    e\n                );\n            } catch (Exception e) {\n                throw new XContentParseException(\n                    null,\n                    \"Failed to build [\" + objectParser.getName() + \"] after last required field arrived\",\n                    e\n                );\n            }\n        }\n    }\n\n    private static class ConstructorArgInfo {\n        final ParseField field;\n        final boolean required;\n\n        ConstructorArgInfo(ParseField field, boolean required) {\n            this.field = field;\n            this.required = required;\n        }\n    }\n}","sourceCodeStart":573,"sourceCodeEnd":609,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java#L573-L609","documentation":"The sibling branch of buildTarget: if the builder BiFunction (or a flushed queued consumer) throws any Exception that is NOT an XContentParseException, it is wrapped with a null location and a capitalized message. This catches generic construction failures such as IllegalArgument/IllegalState/ClassCast from the builder's own validation or from reflective instantiation.","triggerScenarios":"Builder lambda performs casts or range checks that throw IllegalArgumentException, passes wrong-typed args to a constructor, or a queued field consumer mutates state that violates an invariant. Reflective instantiation failures surfaced here when used via InstantiatingObjectParser.","commonSituations":"Builder that asserts numeric ranges or enum membership and throws on invalid input. Type-cast bugs in the builder where the declared ValueType doesn't match the runtime type. Constructor argument count/type mismatch.","solutions":["Read the cause: for IllegalArgumentException it usually states the failed invariant (range, enum, arity) — fix the input or the builder accordingly.","If the cause is a ClassCastException, the declared ValueType for a field does not match what the builder expects; align them.","Prefer failing inside a field consumer with an XContentParseException (which carries location) over throwing raw IllegalArgumentException in the builder."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    Value v = cop.apply(parser, ctx);\n} catch (XContentParseException e) {\n    if (e.getMessage().startsWith(\"Failed to build [\") && e.getLocation() == null) {\n        Throwable root = e.getCause(); // IllegalArgumentException/ClassCastException etc.\n    }\n}","preventionTips":["Align declared ValueType with the builder's expected runtime types to avoid ClassCastException.","Validate ranges/enums in field consumers (with location) rather than letting the builder throw raw IllegalArgumentException.","Pin constructor argument order/types in a unit test so declaration drift is caught at build time."],"tags":["xcontent","constructing-object-parser","builder","deserialization"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}