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 2 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_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_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 @@
%{error | message: error.message || Status.status_message(error.status)}
end

def from_status(401), do: exception(GRPC.Status.unauthorized(), status_message(401))

Check warning on line 74 in lib/grpc/rpc_error.ex

View workflow job for this annotation

GitHub Actions / Check release

GRPC.Status.unauthorized/0 is undefined or private

Check warning on line 74 in lib/grpc/rpc_error.ex

View workflow job for this annotation

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

GRPC.Status.unauthorized/0 is undefined or private

Check warning on line 74 in lib/grpc/rpc_error.ex

View workflow job for this annotation

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

GRPC.Status.unauthorized/0 is undefined or private

Check warning on line 74 in lib/grpc/rpc_error.ex

View workflow job for this annotation

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

GRPC.Status.unauthorized/0 is undefined or private

Check warning on line 74 in lib/grpc/rpc_error.ex

View workflow job for this annotation

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

GRPC.Status.unauthorized/0 is undefined or private

Check warning on line 74 in lib/grpc/rpc_error.ex

View workflow job for this annotation

GitHub Actions / Interop tests

GRPC.Status.unauthorized/0 is undefined or private
def from_status(403), do: exception(GRPC.Status.permission_denied(), status_message(403))
def from_status(404), do: exception(GRPC.Status.unimplemented(), status_message(404))
def from_status(429), do: exception(GRPC.Status.unavailable(), status_message(429))

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

def from_status(status) do
exception(GRPC.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
Loading