Skip to content

Translate errors from HTTP statuses #362

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

Open
wants to merge 6 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
12 changes: 2 additions & 10 deletions lib/grpc/client/adapters/gun.ex
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,7 @@ defmodule GRPC.Client.Adapters.Gun do
)}
end
else
{:error,
GRPC.RPCError.exception(
GRPC.Status.internal(),
"status got is #{status} instead of 200"
)}
{:error, GRPC.RPCError.from_http_status(status)}
end

{:response, :nofin, status, headers} ->
Expand All @@ -266,11 +262,7 @@ defmodule GRPC.Client.Adapters.Gun do
{:response, headers, :nofin}
end
else
{:error,
GRPC.RPCError.exception(
GRPC.Status.internal(),
"status got is #{status} instead of 200"
)}
{:error, GRPC.RPCError.from_http_status(status)}
end

{:data, :fin, data} ->
Expand Down
17 changes: 17 additions & 0 deletions lib/grpc/rpc_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ defmodule GRPC.RPCError do
%{error | message: error.message || Status.status_message(error.status)}
end

def from_http_status(401), do: exception(Status.unauthenticated(), status_message(401))
def from_http_status(403), do: exception(Status.permission_denied(), status_message(403))
def from_http_status(404), do: exception(Status.unimplemented(), status_message(404))
def from_http_status(429), do: exception(Status.unavailable(), status_message(429))

def from_http_status(status) when status in [502, 503, 504] do
exception(Status.unavailable(), status_message(status))
end

def from_http_status(status) do
exception(Status.internal(), status_message(status))
end

defp status_message(status) do
"got http status #{status} instead of 200"
end

defp parse_args([], acc), do: acc

defp parse_args([{:status, status} | t], acc) when is_integer(status) do
Expand Down
26 changes: 26 additions & 0 deletions test/grpc/integration/stub_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@
end)
end

error_table = [
{401, GRPC.Status.unauthenticated()},
{403, GRPC.Status.permission_denied()},
{404, GRPC.Status.unimplemented()},
{429, GRPC.Status.unavailable()},
{502, GRPC.Status.unavailable()},
{503, GRPC.Status.unavailable()},
{504, GRPC.Status.unavailable()},
# General case
{518, GRPC.Status.internal()}
]

client_adapters = [GRPC.Client.Adapters.Gun, GRPC.Client.Adapters.Mint]

for {http_code, expected_error_code} <- error_table, client_adapter <- client_adapters do
test "#{client_adapter} returns RPC Error when getting HTTP #{http_code}" do

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.12.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 401 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.12.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 404 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.12.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 403 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.12.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 503 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.12.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 504 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.12.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 518 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.12.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 429 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.12.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 502 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 403 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 401 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 518 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 502 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 504 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 404 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 429 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 503 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 504 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 403 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 429 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 404 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 401 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 502 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 503 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 518 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 26.1.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 404 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 26.1.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 401 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 26.1.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 504 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 26.1.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 502 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 26.1.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 503 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 26.1.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 518 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 26.1.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 403 (GRPC.Integration.StubTest)

Check failure on line 48 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 26.1.x / Elixir 1.15.x

test Elixir.GRPC.Client.Adapters.Mint returns RPC Error when getting HTTP 429 (GRPC.Integration.StubTest)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure where I should put these tests

Copy link
Contributor

Choose a reason for hiding this comment

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

This seems fine

run_error_server(unquote(http_code), fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}", adapter: unquote(client_adapter))
req = %Helloworld.HelloRequest{name: "GRPC"}

{:error, %GRPC.RPCError{status: unquote(expected_error_code)}} =
Helloworld.Greeter.Stub.say_hello(channel, req)
end)
end
end

test "you can disconnect stubs" do
run_server(HelloServer, fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
Expand Down Expand Up @@ -69,7 +95,7 @@
end

test "invalid channel function clause error" do
req = Helloworld.HelloRequest.new(name: "GRPC")

Check warning on line 98 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.12.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 98 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.15.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 98 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.15.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 98 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 26.1.x / Elixir 1.15.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

assert_raise FunctionClauseError, ~r/Helloworld.Greeter.Stub.say_hello/, fn ->
Helloworld.Greeter.Stub.say_hello(nil, req)
Expand Down
28 changes: 28 additions & 0 deletions test/support/integration_test_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@ defmodule GRPC.Integration.TestCase do
end
end

defmodule ErrorHandler do
@behaviour :cowboy_handler

@impl :cowboy_handler
def init(req, error_code) do
req = :cowboy_req.reply(error_code, %{"content-type" => "text/plain"}, "", req)

{:ok, req, error_code}
end
end

def run_error_server(error_code, func, port \\ 0) do
dispatch =
:cowboy_router.compile([
{:_, [{:_, ErrorHandler, error_code}]}
])

{:ok, _} = :cowboy.start_clear("error_server", [port: port], %{env: %{dispatch: dispatch}})
port = :ranch.get_port("error_server")

try do
func.(port)
:ok
after
:ok = :cowboy.stop_listener("error_server")
end
end

def run_server(servers, func, port \\ 0, opts \\ []) do
{:ok, _pid, port} = GRPC.Server.start(servers, port, opts)

Expand Down
Loading