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 all commits
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
16 changes: 10 additions & 6 deletions lib/grpc/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,16 @@ defmodule GRPC.Server do
) do
GRPC.Telemetry.server_span(server, endpoint, func_name, stream, fn ->
last = fn r, s ->
reply = apply(server, func_name, [r, s])

if res_stream do
{:ok, stream}
else
{:ok, stream, reply}
case apply(server, func_name, [r, s]) do
%GRPC.RPCError{} = error ->
{:error, error}

reply ->
if res_stream do
{:ok, stream}
else
{:ok, stream, reply}
end
end
end

Expand Down
26 changes: 10 additions & 16 deletions lib/grpc/server/adapters/cowboy/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,8 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
result =
try do
case do_call_rpc(server, path, stream) do
{:error, _} = err ->
err

_ ->
:ok
{:error, _} = err -> err
_ -> :ok
end
catch
kind, e ->
Expand All @@ -361,14 +358,8 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
end

case result do
{:error, %GRPC.RPCError{} = e} ->
exit({e, ""})

{:error, %{kind: kind}} ->
exit({:handle_error, kind})

other ->
other
{:error, %GRPC.RPCError{} = e} -> exit({e, ""})
:ok -> :ok
end
end

Expand All @@ -387,8 +378,11 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
GRPC.Server.send_trailers(stream, @default_trailers)
{:ok, stream}

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

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

Expand Down Expand Up @@ -422,7 +416,7 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
:cowboy_req.reply(200, trailers, req)
end

def exit_handler(pid, reason) do
defp exit_handler(pid, reason) do
if Process.alive?(pid) do
Process.exit(pid, reason)
end
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