Translate to: 简体中文
go-error-generator is a plug-in that automatically generates Error through the Enum object of the protobuf file, and easily realizes the i18n of error by defining multiple options in the extended EnumValueOptions
In a front-end and back-end separation project, the error message returned by the backend needs to return different err msg according to different platforms, for example, return to Chinese on the Chinese platform, and return to English on the English platform, but I don’t want to use the automatic translation plug-in to solve it (automatic translation plug-ins sometimes inaccurate translation), Instead, I want to customize err msg for each platform. Is there any package on the market that implements this function, so go-error-generator was born
go-error-generator extracts the custom option in the extended google.protobuf.EnumValueOptions (including option name[platform], option number) and the EnumValueOptions in the protobuf file of the custom error ( Including option number, option value[err msg]), the two are associated through option number, and finally the mapping between option name and option value is obtained, that is, the mapping between different platforms and err msg is obtained.
If you want to get the value of EnumValueOption from protobuf, the general method is:
msg := ""
eMsg := proto.GetExtension(v.Desc.Options(),errors.E_MsgEnglish)
msg = eMsg.(string)
Among them, errors.E_MsgEnglish is an object generated by protoc-gen-go by extending the google.protobuf.EnumValueOptions file. Every time you change the name of the extended option or add an extended option, you need to regenerate and reference the object and recompile the plug-in. Not an elegant implementation, and go-error-generator will read the latest EnumValueOptions extension from the error description file each time it runs, no need to recompile, which is a more flexible implementation
error description file(errors.proto):
syntax = "proto3";
package errors;
option go_package = "github.com/classtorch/go-error-generator-examples/internal/errors";
import "google/protobuf/descriptor.proto";
message Error {
int32 code = 1;
string msg = 2;
};
extend google.protobuf.EnumValueOptions {
string msg = 1108;
string msg_english = 1109;
}
custom err code和msg file(account/errors.proto):
syntax = "proto3";
package uclass.service.account;
option go_package = "/golang/account";
import "errors/errors.proto";
enum ErrorCode {
ACCOUNT_NOT_EXISTS = 0 [(errors.msg) = "账号不存在", (errors.msg_english) = "account not exist"];
}
Generated go code:
// Code generated by protoc-gen-go-error-generator. DO NOT EDIT.
// Version v1.0.0
package account
import (
errors "github.com/classtorch/go-error-generator-examples/internal/errors"
)
var (
ACCOUNT_NOT_EXISTS = &errors.Error{Code: 0, Msg: "账号不存在"} //账号不存在
)
var (
Msg = map[int32]*errors.Error{
0: &errors.Error{Code: 0, Msg: "账号不存在"},
}
Msg_English = map[int32]*errors.Error{
0: &errors.Error{Code: 0, Msg: "account not exist"},
}
)
In the code:
- ACCOUNT_NOT_EXISTS can be directly used in business logic functions to return as go error, provided that the error description file (errors.proto) is used to generate go code, and the generated Error structure must implement the go error interface
- The two maps Msg and Msg_English are the error maps of the Chinese and English platforms, and the Msg of the error is the error text of the current platform
- Automatically generate error according to errCode and msg defined by Enum;
- Support to define multiple EnumValueOption to realize multi-language;
- Support error merge function;
- Support custom Error struct, errorCode and Msg names;
- descriptor_file the error description file path,required
- merge_error Whether all generated errors are merged into one map,the default is false
- merge_error_path Generate merge error file path,required if merge_error=true
go install github.com/classtorch/go-error-generator/protoc-gen-go-error-generator
protoc --go-error-generator_out=:. \
--go-error-generator_opt descriptor_file=errors/errors.proto \
--go-error-generator_opt merge_error=false \
--go-error-generator_opt merge_error_path=golang/errors \
--go_out=. -I . protobuf/**/errors.proto