go data_error ai_generated true

rpc 错误:code = InvalidArgument desc = proto: User: 字段 "email" 为必填

rpc error: code = InvalidArgument desc = proto: User: field "email" is required

ID: go/grpc-failed-precondition-required-field

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2024-12-02首次发现

版本兼容性

版本状态引入弃用备注
github.com/envoyproxy/protoc-gen-validate 1.x active

根因分析

protoc-gen-validate(PGV)或较新的 protovalidate 规则因必填字段为空而拒绝该消息。服务端返回带验证消息的 InvalidArgument。

English

protoc-gen-validate (PGV) or the newer protovalidate rules rejected the message because a required field was empty. The server returns InvalidArgument with the validation message.

generic

解决方案

  1. 90% 成功率
    if err := user.Validate(); err != nil {
        return fmt.Errorf("invalid user: %w", err)
    }
    resp, err := client.Create(ctx, user)
  2. 85% 成功率
    st := status.New(codes.InvalidArgument, "validation failed")
    br := &errdetails.BadRequest{FieldViolations: []*errdetails.BadRequest_FieldViolation{
        {Field: "email", Description: "required"},
    }}
    st, _ = st.WithDetails(br)
    return nil, st.Err()

无效尝试

常见但无效的做法:

  1. 90% 失败

    Removes all input safety checks and allows malformed data into downstream systems.

  2. 85% 失败

    PGV rules like min_len:1 still reject empty strings; only non-empty values pass.