码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 如何使用grpc从服务端像向客户端传递错误信息


     当我们在使用grpc来提供服务的时候,不免会返回某些错误的信息,grpc为我们提供了定义好的错误码,错误码也非常有限。

    以 服务端:go   客户端:python 为例

    服务端返回错误信息

    return status.Errorf(codes.InvalidArgument,"invalid args: %v" +err)

    客户端解析错误信息

    1. # 1、一般返回的是服务端自定义字符串信息
    2. e.details()
    3. # 2、一般是grpc的状态码信息
    4. status_code = e.code()
    5. status_code.name # 对应状态码名字(string)
    6. status_code.value # 对应状态码(int)

    grpc为我们提供了16种“开箱即用”的状态码

    但是往往我们需要的错误信息是与我们的系统数据相关:所以如何实现扩展呢

    1、通过自定义状态码来实现?

    1. //方式一
    2. return status.Error(codes.Code(5200), "create failed ")
    3. //方式二
    4. const(
    5. UserNotExist codes.Code = 5200
    6. )
    7. return status.Error(UserNotExist, "create failed ")

    实际上 ,客户端接收error时,code信息是2,unknown(这是因为我们自定义的状态码并不存在于grpc约定好的错误码信息中)

    2、通过定义proto的message来实现?

    Google 基于自身业务, 有了一套错误扩展 https://cloud.google.com/apis/design/errors#error_model, 简单来说就是自己定义了一个 protobuf 错误消息类型:

    1. // The `Status` type defines a logical error model that is suitable for
    2. // different programming environments, including REST APIs and RPC APIs.
    3. message Status {
    4. // A simple error code that can be easily handled by the client. The
    5. // actual error code is defined by `google.rpc.Code`.
    6. int32 code = 1;
    7. // A developer-facing human-readable error message in English. It should
    8. // both explain the error and offer an actionable resolution to it.
    9. string message = 2;
    10. // Additional error information that the client code can use to handle
    11. // the error, such as retry info or a help link.
    12. repeated google.protobuf.Any details = 3;
    13. }

    此段代码转自:gRPC 扩展错误处理 - 墨天轮 (modb.pro)

    那么问题来了, 如何传递这个非标准的错误扩展消息呢? 答案是放在 trailing response metadata
    中, key 为 grpc-status-details-bin

    此方法搭配 status.Details();但我没有试验成功

    s, _ := status.Newf(codes.InvalidArgument, "invalid input").WithDetails(&Status)

    这个方法还能将具体的proto字段指定出来并增加描述

    1. s, _ := status.Newf(codes.InvalidArgument, "invalid input").WithDetails(&errdetails.BadRequest{
    2. FieldViolations: []*errdetails.BadRequest_FieldViolation{
    3. {
    4. Field: "SomeRequest.email_address",
    5. Description: "INVALID_EMAIL_ADDRESS",
    6. },
    7. {
    8. Field: "SomeRequest.username",
    9. Description: "INVALID_USER_NAME",
    10. },
    11. },
    12. })
    13. return s.Err()

  • 相关阅读:
    scikit-learn机器学习算法封装
    SLAM视觉里程计的理解
    ProEasy机器人:TCP无协议通讯(socket通讯)时打印log日志
    laravel composer require require-dev和APP_ENV的使用场景
    01.Gameplay Architecture ECS简介
    现今主流物联网无线通信技术分类详解
    【智慧排水】智慧排水监测系统助力城市抗洪排涝建设
    给定一个数组arr,长度为N且每个值都是正数,代表N个人的体重。再给定一个正数 limit,代表一艘船的载重。
    痞子衡嵌入式:IAR内部C-SPY调试组件配套宏文件(.mac)用法介绍
    【023】Springboot+vue+mysql员工考勤管理系统(多角色登录、请假、打卡)(含源码、数据库、运行教程)
  • 原文地址:https://blog.csdn.net/qq_40604313/article/details/126105265
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号