Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting status code in grpc server method call

Tags:

go

grpc

How do I set the response status code in a grpc method in golang. For example lets say I have the following grpc method

func (i *ItemServerImp) Register(ct context.Context, it *item.RegisterItemRequest) (*item.RegisterItemReply, error) {
}

How do I set the response status to 200 or a 400 based on the input or some processing. I had a look around and could not find a proper way to do this.

However I did find the following https://chromium.googlesource.com/external/github.com/grpc/grpc/+/refs/heads/chromium-deps/2016-07-27/doc/statuscodes.md which says the status code can be set.

like image 570
tmp dev Avatar asked May 29 '26 11:05

tmp dev


1 Answers

You can return a gRPC error using the google.golang.org/grpc/status package as follows:

return nil, status.Error(codes.InvalidArgument, "Incorrect request argument")

The different status codes are available in the google.golang.org/grpc/codes package.

like image 173
Gregor Zurowski Avatar answered May 31 '26 06:05

Gregor Zurowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!