{"record":{"id":"400ad562f0cce405","repo":"alibaba/nacos","slug":"can-t-get-single-data-from-batched-data","errorCode":null,"errorMessage":"Can't get single data from batched data.","messagePattern":"Can't get single data from batched data\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"client/src/main/java/com/alibaba/nacos/client/ai/remote/redo/AgentEndpointWrapper.java","lineNumber":57,"sourceCode":"        this.data = copyEndpoints(data);\n        this.isBatch = isBatch;\n    }\n    \n    public static AgentEndpointWrapper wrap(AgentEndpoint data) {\n        return new AgentEndpointWrapper(Collections.singletonList(data), false);\n    }\n    \n    public static AgentEndpointWrapper wrap(Collection<AgentEndpoint> data) {\n        return new AgentEndpointWrapper(data, true);\n    }\n    \n    public boolean isBatch() {\n        return isBatch;\n    }\n    \n    public AgentEndpoint getData() {\n        if (isBatch) {\n            throw new UnsupportedOperationException(\"Can't get single data from batched data.\");\n        }\n        return data.iterator().next();\n    }\n    \n    public Collection<AgentEndpoint> getBatchData() {\n        if (!isBatch) {\n            throw new UnsupportedOperationException(\"Can't get batched data from single data.\");\n        }\n        return data;\n    }\n    \n    /**\n     * Return the exact Agent Version shared by this wrapper's Endpoint payload.\n     *\n     * @return exact Version\n     */\n    public String getVersion() {\n        return data.iterator().next().getVersion();","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/client/src/main/java/com/alibaba/nacos/client/ai/remote/redo/AgentEndpointWrapper.java#L39-L75","documentation":"Thrown as UnsupportedOperationException by AgentEndpointWrapper.getData() when the wrapper was created in batch mode (via wrap(Collection<AgentEndpoint>)). getData() is the single-item accessor and is only valid on wrappers created via wrap(AgentEndpoint). This is a programming error indicating a type/access-pattern mismatch.","triggerScenarios":"Code calls wrapper.getData() on a wrapper instance that was created by AgentEndpointWrapper.wrap(Collection<AgentEndpoint>) — i.e., the wrapper holds a collection of endpoints but the caller expects a single endpoint. This typically happens when agent endpoint registration or redo logic processes batch registrations but a code path assumes a single endpoint.","commonSituations":"Agent endpoint redo/registration code path where batch registrations (AgentEndpointRegistrationBatch) are processed. A developer assumes a single-endpoint response but the wrapper was populated from a batch source. Refactoring that changed the wrap() call site from single to batch without updating the consumer.","solutions":["Check wrapper.isBatch() before calling getData() and branch to getBatchData() when true.","Trace where the wrapper was created — if it should be single, use wrap(AgentEndpoint) instead of wrap(Collection).","If the consumer genuinely needs a single endpoint from a batch, call getBatchData() and pick the relevant entry explicitly."],"exampleFix":"// before: assumes single, throws on batch wrapper\nAgentEndpoint ep = wrapper.getData();\n\n// after: check batch flag first\nAgentEndpoint ep;\nif (wrapper.isBatch()) {\n    Collection<AgentEndpoint> endpoints = wrapper.getBatchData();\n    ep = endpoints.iterator().next(); // or select by criteria\n} else {\n    ep = wrapper.getData();\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Type guard: check isBatch before accessing single-item data\npublic AgentEndpoint safeGetSingle(AgentEndpointWrapper wrapper) {\n    if (wrapper.isBatch()) {\n        throw new IllegalStateException(\"Expected single endpoint, got batch\");\n    }\n    return wrapper.getData();\n}","tryCatchPattern":"try {\n    AgentEndpoint ep = wrapper.getData();\n} catch (UnsupportedOperationException e) {\n    // wrapper is in batch mode — use getBatchData() instead\n    log.warn(\"Wrapper is batch, falling back to getBatchData()\");\n    Collection<AgentEndpoint> batch = wrapper.getBatchData();\n}","preventionTips":["Always call isBatch() before getData() or getBatchData().","Document the expected mode at the wrapper creation site.","Use factory methods consistently: wrap(AgentEndpoint) for single, wrap(Collection) for batch."],"tags":["nacos","ai-client","agent-endpoint","type-mismatch","programming-error"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}