protobuf-net / protobuf-net.Grpc

GRPC bindings for protobuf-net and grpc-dotnet
Other
857 stars 109 forks source link

[Question] Exception in catch block always null #212

Open williammendat opened 2 years ago

williammendat commented 2 years ago

Hello, and sorry that i am making an issue for that...

I am currently working on a Blazor WASM project with grpc web code first approach. Everything seems to work at this point but when it comes to exception handling, the exception on the client side to handle it, is always null. I dont know, maybe i messed up the configuration or something...

Here is my code:

ServerLoggerInterceptor:

public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
            TRequest request,
            ServerCallContext context,
            UnaryServerMethod<TRequest, TResponse> continuation)
        {
            //LogCall<TRequest, TResponse>(MethodType.Unary, context);

            try
            {
                return await continuation(request, context);
            }
            catch (RpcException ex)
            {
                var httpContext = context.GetHttpContext();
                httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;

                throw new RpcException(ex.Status);
            }
        }

Server Startup:

services.AddCodeFirstGrpc(options =>
            {
                {
                    options.Interceptors.Add<ServerLoggerInterceptor>();
                    options.EnableDetailedErrors = true;
                }
            });

Server Methode:

public async ValueTask<PeopleReply> GetAll(GetAllPeopleRequest request, CallContext context = default)
        {
            var test = context.ServerCallContext.GetHttpContext().User.GetUserID();
            var user = await _userService.GetUserAsync(test);

            if (user.FirstName != "Test")
                throw new RpcException(new Status(StatusCode.NotFound, "User not found")); 

            var reply = new PeopleReply();
            reply.Persons.AddRange(_peopleList);
            return await new ValueTask<PeopleReply>(reply);
        }

Client Methode:

private async Task GRPCButtonClicked()
        {
            GRPCResult = "Loading...";
            await InvokeAsync(StateHasChanged);
            try
            {
                var startTime = DateTime.Now;
                var list = await PersonService.GetAll(new GetAllPeopleRequest());
                if (list != null)
                {
                    _peopleList = list.Persons.ToList();
                    var elapsed = DateTime.Now.Subtract(startTime);
                    GRPCResult = $"{_peopleList.Count} records returned via gRPC in {elapsed.TotalMilliseconds} ms.";
                    await InvokeAsync(StateHasChanged);
                }
            }
            catch (RpcException ex)
            {
                Console.WriteLine(ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

Note: I am getting in the catch block but "ex" is null.

mgravell commented 2 years ago

Sorry, Xmas.

On the surface, that sounds ... honestly, impossible. Can I just confirm: we're saying that ex in the catch is null? If so: which of the two catch? I wonder if this is a WASM peculiarity, and WASM doesn't enforce quite the same runtime rules on exceptions that we now expect.

williammendat commented 2 years ago

Sooo now sorry for the late answer....

Yes the Exception is null in

catch (RpcException ex)
            {
                Console.WriteLine(ex);
            }

It is also interessting that in the second catch block the Exception is not null but i cant really work with the informations from there.

I wanted to make an example project but i couldnt get it working quickly, so i prepared an old project with grpc code first.

Because its an old project, there is unnessary code for this issue.

The focus is in the "Person" page, there is a button "Test gRPC" which calls a grpc method where an RpcException is thrown.

To get this project run you have to first start the API project, then the client.

After starting the Client, it will start with a login page. There i have it modified, that you can just login with out typing anything.

The project can be found here: https://github.com/Madara789/BlazorYoutubeDl

Dunge commented 7 months ago

Hey @williammendat did you have any result with this issue? I'm seeing a similar thing: image

Also in Blazor WASM

Edit: Ok with a Console.WriteLine(ex.ToString()); I do see the error in the browser console:

Grpc.Core.RpcException: Status(StatusCode="Cancelled", Detail="Bad gRPC response. Invalid content-type value: text/html; charset=utf-8")

I guess this is just some Visual Studio WASM Debugger link issue.