{
  "id": "go/grpc-internal-error-transport",
  "signature": "rpc error: code = Internal desc = grpc: error while marshaling: proto: Marshal called with nil",
  "signature_zh": "rpc 错误：code = Internal desc = grpc：序列化时出错：proto：Marshal 调用时传入 nil",
  "regex": "rpc\\ error:\\ code\\ =\\ Internal\\ desc\\ =\\ grpc:\\ error\\ while\\ marshaling:\\ proto:\\ Marshal\\ called\\ with\\ nil",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The gRPC framework failed to marshal the response because a nil message was returned from the handler. The nil propagates to the codec.",
  "root_cause_type": "generic",
  "root_cause_zh": "gRPC 框架无法序列化响应，因为处理程序返回了 nil 消息。nil 传播到了编解码器。",
  "versions": [
    {
      "version": "grpc-go 1.60+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "A nil response is a contract violation; a custom codec hides the bug and produces empty/garbage payloads.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Returning a non-nil but empty message masks the real logic error and clients receive semantically wrong data.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Always return a non-nil response or an explicit error from handlers:\nfunc (s *svc) Get(ctx context.Context, req *pb.Req) (*pb.Resp, error) {\n    if req == nil {\n        return nil, status.Error(codes.InvalidArgument, \"nil request\")\n    }\n    return &pb.Resp{...}, nil\n}",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Add a server interceptor to detect nil responses early:\nfunc nilGuard(ctx context.Context, req, resp any, info *grpc.UnaryServerInfo, h grpc.UnaryHandler) (any, error) {\n    r, err := h(ctx, req)\n    if err == nil && r == nil { return nil, status.Error(codes.Internal, \"handler returned nil\") }\n    return r, err\n}",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-02-14",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}