-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix protoc-gen-go-errors bug with code OK && improve protoc-gen-go-er…
…rors docs
- Loading branch information
Showing
9 changed files
with
251 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# protoc-gen-go-errors 使用说明 | ||
|
||
## 使用准备 | ||
- 需要本地提前安装好 protoc google/protobuf 等 | ||
- 需要下载最新的 protoc-gen-go-errors 插件,并添加到本地环境变量 | ||
|
||
## 定义错误 proto 文件 | ||
```protobuf | ||
syntax = "proto3"; | ||
package biz.v1; | ||
// 下行注解是必要的,有这个注解 protoc-gen-go-errors 才尝试解析当前 protobuf 文件中的 enum,并基于 enum 生成错误桩代码 | ||
// @plugins=protoc-gen-go-errors | ||
// language-specified package name | ||
option go_package = "biz/v1;bizv1"; | ||
option java_multiple_files = true; | ||
option java_outer_classname = "BizProto"; | ||
option java_package = "com.ego.biz.v1"; | ||
// enum Err 定义了错误的不同枚举值,protoc-gen-go-errors 会基于 enum Err 枚举值生成错误桩代码 | ||
// @code 为错误关联的gRPC Code (遵循 https://grpc.github.io/grpc/core/md_doc_statuscodes.html 定义,需要全大写), | ||
// 包含 OK、UNKNOWN、INVALID_ARGUMENT、PERMISSION_DENIED等 | ||
// @i18n.cn 国际化中文文案 | ||
// @i18n.en 国际化英文文案 | ||
enum Err { | ||
// 请求正常,实际上不算是一个错误 | ||
// @code=OK | ||
// @i18n.cn="请求成功" | ||
// @i18n.en="OK" | ||
ERR_OK = 0; | ||
// 未知错误,比如业务panic了 | ||
// @code=UNKNOWN # 定义了这个错误关联的gRPC Code为:UNKNOWN | ||
// @i18n.cn="服务内部未知错误" # 定义了一个中文错误文案 | ||
// @i18n.en="unknown error" # 定义了一个英文错误文案 | ||
ERR_UNKNOWN = 1; | ||
// 找不到指定用户 | ||
// @code=NOT_FOUND | ||
// @i18n.cn="找不到指定用户" | ||
// @i18n.en="user not found" | ||
ERR_USER_NOT_FOUND = 2; | ||
// 用户ID不合法 | ||
// @code=INVALID_ARGUMENT | ||
// @i18n.cn="用户ID不合法" | ||
// @i18n.en="invalid user id" | ||
ERR_USER_ID_NOT_VALID = 3; | ||
} | ||
``` | ||
|
||
## 使用 | ||
```bash | ||
protoc --proto_path=. --go_out=paths=source_relative:. --go-errors_out=paths=source_relative:. ./errors.proto | ||
``` | ||
|
||
详细用例可以参考 [./error.sh](./error.sh) 脚本 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.