Reputation: 404
I have a grpc handler Something(ctx context.Context, request *protocol.Something) (*pb.Test, error)
I return errors like return nil, status.Error(codes.InvalidArgument, "something wrong")
In case of success. I always return nil return test, nil
, although there is code 0
. Do I have to return the code on success? return test, status.New(codes.OK, "OK")
Upvotes: 2
Views: 1919
Reputation: 48536
Per doc - return code 0
means not an error; returned on success.
Code | Number | Description |
---|---|---|
OK | 0 | Not an error; returned on success. |
Through return test, nil
, the nil in the error, means there is no error, and OK
is returned on success
// OK is returned on success.
OK Code = 0
As you mentioned in the question, return test, status.New(codes.OK, "OK")
, actually, the status.New()
just return Status
rather than error
, it could be failed in the function Something
.
You may use status.Error(codes.OK, "OK")
which return error
. However, if codes.OK
is passed in, returns nil
. It is the same behavior as return nil
directly.
Source code
// Error returns an error representing c and msg. If c is OK, returns nil.
func Error(c codes.Code, msg string) error {
return New(c, msg).Err()
}
Upvotes: 3
Reputation: 1521
No you don't need to do that - no need to return the status code ok on success
Upvotes: 0