go data_error ai_generated true

rpc error: code = OutOfRange desc = index 10 out of bounds for list of length 5

ID: go/grpc-out-of-range-index-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-12-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.1 active

Root Cause

Client requested an index that exceeds the size of a list or array in the response.

generic

中文

客户端请求的索引超出了响应中列表或数组的大小。

Workarounds

  1. 95% success Validate the index before making the request.
    if idx < 0 || idx >= len(items) {
        return fmt.Errorf("index out of range")
    }
    resp, err := client.GetItem(ctx, &pb.GetRequest{Index: int32(idx)})
  2. 90% success Fetch the entire list and then access the index locally.
    listResp, err := client.GetList(ctx, &pb.ListRequest{})
    if err != nil { return err }
    if idx < 0 || idx >= len(listResp.Items) {
        return fmt.Errorf("index out of range")
    }
    item := listResp.Items[idx]

Dead Ends

Common approaches that don't work:

  1. Assume the server will handle out-of-range gracefully. 100% fail

    The server returns an error; it does not auto-correct.

  2. Use negative indexing hoping it wraps around. 90% fail

    Protobuf does not support negative indexing; it will still fail.