protoc-gen-go-errors
This protobuf plugins enables rpc method to list possible errors returned. The
plugin will generate single, package level, Go error variable for every error
listed. Server may use the generated error variable as return error and client
Can catch the error with errors.Is()
assertion.
Every error definition MUST contain two properties:
-
arbitrary error code string
This can be served as human friendly error identifier
-
an enumerated gRPC status code
This will be transalated into real gRPC status code which will be used over the wire.
An simple example will be like below
syntax="proto3";
import "protoc-gen-go-errors/proto/error.proto"
// ...
service Sample {
rpc Method(Input) returns (Response) {
option(errors.types) = {
status: ALREADY_EXISTS,
code: "err_email_exists"
};
option(errors.types) = {
status: INVALID_ARGUMENT,
code: "err_invalid_email"
};
}
}
Installation
- Install protoc-gen-go-errors
$ go install github.com/ncrypthic/proto-gen-go-errors
- Generate error definition Golang file
protoc --go-errors_out=:<dir> -I <path to protoc-gen-go-errors parent dir> -I <path to proto file directory> <proto_file_name>
LICENSE
GNU General Public License v3.0 or later
See COPYING to see the full text.