question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Returning null from a grpc interceptor when there is a failure status code

See original GitHub issue

Why can’t return null from an interceptor there is a failure status code.

public class TestInterceptor : Interceptor
{
    public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod<TRequest, TResponse> continuation)
    {
        if (Some Logic)
        {
            return await  continuation(request, context);
        }
        context.Status = new Status(StatusCode.Unavailable, "Some information about the issue");
        return null!;
    }
}

I don’t think I’m supposed to return a response when there is a failure status code. Or do I? When I return null with failure status code I get this error message

Grpc.Core.RpcException: Status(StatusCode="Cancelled", Detail="No message returned from method.")
at Grpc.AspNetCore.Server.Internal.CallHandlers.UnaryServerCallHandler`3.HandleCallAsyncCore(HttpContext httpContext, HttpContextServerCallContext serverCallContext)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
mayukicommented, Mar 3, 2022

We use MessagePack for Marshaller instead of protobuf, so it is possible to serialize null. This means that a Unary method implementation that returns null as TResponse in Method<TRequest, TResponse> can still send a message body.

However, as discussed here, Grpc.AspNetCore.Server will block if it receives null.😢

1reaction
JamesNKcommented, Jan 20, 2022

Yeah, the performance difference for the exceptions is most of the time is unnoticeable and negligible. But what about other times?

gRPC doesn’t use exceptions in typical application flow. If an HTTP request is being canceled, causing gRPC to throw a RpcException, an exception being thrown will have no impact at all on performance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - What is the proper way to return an error from gRPC ...
I have a Quarkus application that uses gRPC. I have implemented a ServerInterceptor to look for and validate auth tokens. @ApplicationScoped ...
Read more >
Getting Error Handling right in gRPC
gRPC has limited built-in error handling based on simple status codes and metadata, ... so error.getCause() on the client is effectively returning null...
Read more >
Getting Error Handling right in gRPC | by Pankaj Kumar
By default, gRPC relies heavily on status code for error handling. ... so error.getCause() on the client is effectively returning null .
Read more >
How to handle gRPC errors in .Net Core - the-worst.dev
Handling gRPC server exceptions in .Net Core. Changing of gRPC requests HTTP status code for bad requests. Transform gRPC exception details ...
Read more >
Error Handling in gRPC
In this tutorial, we'll focus on gRPC error handling using Java. gRPC has very low latency and high throughput, so it's ideal to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found