# rpc 错误：code = Internal desc = grpc: 服务器无法编码响应：rpc 错误：code = Internal desc = grpc: 编组时出错：proto: 使用 nil 调用 Marshal

- **ID:** `go/grpc-internal-error-server-panic`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

服务器处理程序返回了 nil 响应消息，gRPC 框架尝试编组它，导致 panic 或错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   func (s *server) YourMethod(ctx context.Context, req *pb.Request) (*pb.Response, error) {
    // Always return a valid response
    return &pb.Response{}, nil
}
   ```
2. **** (90% 成功率)
   ```
   func validateResponseInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    resp, err := handler(ctx, req)
    if err != nil { return nil, err }
    if resp == nil { return nil, status.Error(codes.Internal, "nil response") }
    return resp, nil
}
   ```

## 无效尝试

- **** — Recovering from the panic without fixing the nil response still results in an error sent to the client. (70% 失败率)
- **** — Returning an empty message may be semantically incorrect and does not address the root cause. (50% 失败率)
