Whilst we’d love our code to work perfectly, we also need to handle errors that might come from our server code.
We might simple wrap any response’s in a Result class (or similar) which returns data along with a success or failure status code (or ofcourse include a status code in a response object).
C#, Java etc. also use exceptions, so we might prefer to throw exceptions on the server.
gRPC supports exceptions but with some limitations…
Exceptions come with status codes
Any exceptions thrown on the server will result in RpcException being thrown – these do NOT reflect the exception that was originally thrown, i.e. if your server code threw an ArgumentException the client will only ever see an RpcExecption with a StatusCode of Unknown.
As you can see, an RpcException includes a status code, so obviously our server can instead throw an RpcException and supply the Status and StatusCode along with it, for example
throw new RpcException( new Status( StatusCode.FailedPrecondition, $"Argument {nameof(request)} is invalid"));
Ofcourse handling such exceptions is as simple as
try { var response = client.Query(new NotesReques()) } catch(RpcException e) { Debug.WriteLine(e.Status.Detail); Debug.WriteLine(e.Status.StatusCode); }