{"record":{"id":"caf4d014efc1c2a0","repo":"redis/jedis","slug":"failed-to-fetch-next-aggregation-batch","errorCode":null,"errorMessage":"Failed to fetch next aggregation batch","messagePattern":"Failed to fetch next aggregation batch","errorType":"exception","errorClass":"JedisException","httpStatus":null,"severity":"error","filePath":"src/main/java/redis/clients/jedis/search/aggr/AggregateIterator.java","lineNumber":125,"sourceCode":"  @Override\n  public AggregationResult next() {\n    if (!hasNext()) {\n      throw new NoSuchElementException(\"No more aggregation results available\");\n    }\n\n    try {\n      if (aggrCommandResult != null) {\n        try {\n          return aggrCommandResult;\n        } finally {\n          aggrCommandResult = null;\n        }\n      } else {\n        return doFetch();\n      }\n\n    } catch (Exception e) {\n      throw new JedisException(\"Failed to fetch next aggregation batch\", e);\n    }\n  }\n\n  /**\n   * Returns the current cursor ID.\n   * @return cursor ID, or null if not initialized\n   */\n  public Long getCursorId() {\n    return cursorId;\n  }\n\n  @Override\n  public void remove() {\n    aggrCommandResult = null;\n\n    if (cursorId == null || cursorId <= 0) {\n      // Cursor is already closed or not initialized, nothing to do\n      return;","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/search/aggr/AggregateIterator.java#L107-L143","documentation":"Wraps any exception raised while obtaining the next aggregation batch (either returning the cached first result or executing the cursor's FT.CURSOR read via doFetch) into a JedisException. It indicates a runtime failure — connection error, bad reply, protocol issue — while advancing the aggregation cursor, not a normal end-of-results condition.","triggerScenarios":"Any Exception thrown inside next(): borrowing a connection fails, the cursor command is rejected by the server (e.g. unknown/expired cursor), or the reply cannot be parsed into an AggregationResult.","commonSituations":"Cursor expired on the server (CURSOR TTL elapsed between batches); Redis node became unreachable mid-iteration; cluster resharding moved the cursor; serialization/parse errors on unexpected replies.","solutions":["Catch JedisException and inspect the cause; if the cursor expired, restart the aggregation from the beginning","Ensure iteration completes promptly or re-set a longer CURSOR TTL in the AggregationBuilder if batches are consumed slowly","Check network stability and cluster health between batches; retry the whole aggregation with backoff on transient connection errors","Log the wrapped cause to distinguish protocol/parse failures from connectivity failures"],"exampleFix":"// before\nAggregationResult r = iterator.next(); // raw JedisException propagates\n// after\ntry {\n  AggregationResult r = iterator.next();\n} catch (JedisException e) {\n  // retry aggregation or inspect e.getCause() (expired cursor, connectivity)\n  restartAggregation();\n}","handlingStrategy":"try-catch","validationCode":"// ensure cursor is still valid before continuing if you track it\nLong cursorId = iterator.getCursorId();\nboolean cursorUsable = cursorId == null || cursorId > 0;","typeGuard":"null","tryCatchPattern":"try {\n  AggregationResult r = iterator.next();\n} catch (JedisException e) {\n  Throwable cause = e.getCause();\n  // expired cursor / connectivity: log cause and restart the whole aggregation\n  restartAggregation();\n}","preventionTips":["Consume batches promptly so the server-side cursor TTL doesn't expire; raise CURSOR TTL for slow consumers","Monitor cluster/node health during long aggregations","Log e.getCause() to distinguish expired-cursor from network failures","Wrap the full iteration in a retry that rebuilds the aggregation from scratch"],"tags":["jedis","search","aggregation","cursor","wrapped-exception"],"backgroundTag":"api-request-failed","analyzedSha":"6dac31d4c224fb3257c216f3985340c6f500cdcb","analyzedAt":"2026-09-08T04:55:01.204Z","contentChangedAt":"2026-09-08T04:55:01.204Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}