Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle not exception errors from the server #321

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/grpc/server/adapters/cowboy/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
result = server.__call_rpc__(path, stream)

case result do
{:ok, _stream, error = %GRPC.RPCError{message: nil, status: status}} ->
{:error, %{error | message: GRPC.Status.status_message(status)}}

{:ok, _stream, error = %GRPC.RPCError{}} ->
{:error, error}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe more changes are needed so that we get this errors to bubble up without raising. Currently they still raise on like 364.

The idea is that if we manage to bubble them up, then they'll bypass any exception-handling workflow and they'll also follow all interceptors along instead of cutting the workflow dry after the call

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely I'm wrong, but I'm understanding that those exits are because that part of the code is executed with a spawn_link + Process.flag(:trap_exit, true). I have updated the Server module to return an error tuple that in fact is what I think the logger interceptor expects. I'm missing something no?

{:ok, stream, response} ->
stream
|> GRPC.Server.send_reply(response)
Expand Down
40 changes: 40 additions & 0 deletions test/grpc/integration/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ defmodule GRPC.Integration.ServerTest do
raise "unknown error(This is a test, please ignore it)"
end

def say_hello(%{name: "handled error"}, _stream) do
%GRPC.RPCError{
status: GRPC.Status.unauthenticated(),
message: "Please authenticate"
}
end

def say_hello(%{name: "handled error without message"}, _stream) do
%GRPC.RPCError{
status: GRPC.Status.unauthenticated()
}
end

def say_hello(_req, _stream) do
raise GRPC.RPCError, status: GRPC.Status.unauthenticated(), message: "Please authenticate"
end
Expand Down Expand Up @@ -172,6 +185,33 @@ defmodule GRPC.Integration.ServerTest do
end)
end

test "return errors for handled errors" do
run_server([HelloErrorServer], fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
req = Helloworld.HelloRequest.new(name: "handled error")
{:error, reply} = channel |> Helloworld.Greeter.Stub.say_hello(req)

assert %GRPC.RPCError{
status: GRPC.Status.unauthenticated(),
message: "Please authenticate"
} == reply
end)
end

test "return errors for handled errors with the default message of the status" do
run_server([HelloErrorServer], fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
req = Helloworld.HelloRequest.new(name: "handled error without message")
{:error, reply} = channel |> Helloworld.Greeter.Stub.say_hello(req)

assert %GRPC.RPCError{
status: GRPC.Status.unauthenticated(),
message:
"The request does not have valid authentication credentials for the operation"
} == reply
end)
end

test "returns appropriate error for stream requests" do
run_server([FeatureErrorServer], fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
Expand Down