Skip to content

Commit

Permalink
Add details to grpc error status struct
Browse files Browse the repository at this point in the history
  • Loading branch information
rishab-brex committed Oct 16, 2021
1 parent 2aff9a8 commit 8d8e42b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
22 changes: 15 additions & 7 deletions lib/grpc/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ defmodule GRPC.RPCError do
# server side
raise GRPC.RPCError, status: :unknown # preferred
raise GRPC.RPCError, status: GRPC.Status.unknown, message: "error message"
raise GRPC.RPCError, status: GRPC.Status.unknown, details: [Google.Rpc.LocalizedMessage.new!(locale: “en-US”, message: “User friendly string”)]
# client side
{:error, error} = Your.Stub.unary_call(channel, request)
"""

defexception [:status, :message]
@type t :: %__MODULE__{status: GRPC.Status.t(), message: String.t()}
defexception [:status, :message, :details]
@type t :: %__MODULE__{status: GRPC.Status.t(), message: String.t(), details: [any()]}

alias GRPC.Status

@spec exception(Status.t(), String.t()) :: t
@spec exception(Status.t(), String.t(), [any()]) :: t
def new(status) when is_atom(status) do
exception(status: status)
end
Expand Down Expand Up @@ -47,12 +48,19 @@ defmodule GRPC.RPCError do
parse_args(t, acc)
end

def exception(status, message) when is_atom(status) do
%GRPC.RPCError{status: apply(GRPC.Status, status, []), message: message}
defp parse_args([{:details, details} | t], acc) do
acc = %{acc | details: details}
parse_args(t, acc)
end

def exception(status, message, details \\ [])

def exception(status, message, details) when is_atom(status) do
%GRPC.RPCError{status: apply(GRPC.Status, status, []), message: message, details: details}
end

def exception(status, message) when is_integer(status) do
%GRPC.RPCError{status: status, message: message}
def exception(status, message, details) when is_integer(status) do
%GRPC.RPCError{status: status, message: message, details: details}
end

defp status_message(1), do: "The operation was cancelled (typically by the caller)"
Expand Down
3 changes: 2 additions & 1 deletion test/grpc/integration/codec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ defmodule GRPC.Integration.CodecTest do

assert %GRPC.RPCError{
status: GRPC.Status.unimplemented(),
message: "No codec registered for content-type application/grpc+not-registered"
message: "No codec registered for content-type application/grpc+not-registered",
details: []
} == reply
end)
end
Expand Down
13 changes: 9 additions & 4 deletions test/grpc/integration/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ defmodule GRPC.Integration.ServerTest do

assert %GRPC.RPCError{
status: GRPC.Status.unauthenticated(),
message: "Please authenticate"
message: "Please authenticate",
details: []
} == reply
end)
end
Expand All @@ -120,7 +121,11 @@ defmodule GRPC.Integration.ServerTest do
req = Helloworld.HelloRequest.new(name: "unknown error")

assert {:error,
%GRPC.RPCError{message: "Internal Server Error", status: GRPC.Status.unknown()}} ==
%GRPC.RPCError{
message: "Internal Server Error",
status: GRPC.Status.unknown(),
details: []
}} ==
channel |> Helloworld.Greeter.Stub.say_hello(req)
end)
end
Expand All @@ -129,7 +134,7 @@ defmodule GRPC.Integration.ServerTest do
run_server([FeatureErrorServer], fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
rect = Routeguide.Rectangle.new()
error = %GRPC.RPCError{message: "Please authenticate", status: 16}
error = %GRPC.RPCError{message: "Please authenticate", status: 16, details: []}
assert {:error, ^error} = channel |> Routeguide.RouteGuide.Stub.list_features(rect)
end)
end
Expand All @@ -148,7 +153,7 @@ defmodule GRPC.Integration.ServerTest do
run_server([TimeoutServer], fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
rect = Routeguide.Rectangle.new()
error = %GRPC.RPCError{message: "Deadline expired", status: 4}
error = %GRPC.RPCError{message: "Deadline expired", status: 4, details: []}

assert {:error, ^error} =
channel |> Routeguide.RouteGuide.Stub.list_features(rect, timeout: 500)
Expand Down
3 changes: 2 additions & 1 deletion test/grpc/integration/stub_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ defmodule GRPC.Integration.StubTest do
assert {:error,
%GRPC.RPCError{
message: "Deadline expired",
status: GRPC.Status.deadline_exceeded()
status: GRPC.Status.deadline_exceeded(),
details: []
}} == channel |> Helloworld.Greeter.Stub.say_hello(req, timeout: 500)
end)
end
Expand Down

0 comments on commit 8d8e42b

Please sign in to comment.