Skip to content

Commit

Permalink
[#140] Handle :erpc exception reason {:erpc, reason}
Browse files Browse the repository at this point in the history
  • Loading branch information
cabol committed Oct 27, 2021
1 parent faff154 commit 998a30f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ jobs:
uses: actions/cache@v1
with:
path: priv/plts
key: '${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt-v3'
key: '${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt-v4'
restore-keys: |
${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt-v3
${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt-v4
- name: Run static analysis checks
run: |
Expand Down
26 changes: 26 additions & 0 deletions lib/nebulex/exceptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,29 @@ defmodule Nebulex.RPCMultiCallError do
%__MODULE__{message: message}
end
end

defmodule Nebulex.RPCError do
@moduledoc """
Raised at runtime when a RPC error occurs.
"""

@type t :: %__MODULE__{reason: atom}

defexception [:reason]

@impl true
def message(%__MODULE__{reason: reason}) do
format_reason(reason)
end

# :erpc.call/5 doesn't format error messages.
defp format_reason({:erpc, _} = reason) do
"""
The RPC operation failed with reason:
#{inspect(reason)}
See :erpc.call/5 for more information about the error reasons.
"""
end
end
3 changes: 3 additions & 0 deletions lib/nebulex/rpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ defmodule Nebulex.RPC do

{:exception, original, _} ->
:erlang.raise(:error, original, __STACKTRACE__)

other ->
reraise %Nebulex.RPCError{reason: other}, __STACKTRACE__
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/nebulex/adapters/partitioned_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ defmodule Nebulex.Adapters.PartitionedTest do
end
end

test ":erpc error" do
assert Partitioned.put_all(for(x <- 1..100_000, do: {x, x}), timeout: 60_000) == :ok
assert Partitioned.get(1, timeout: 1000) == 1

msg = ~r"The RPC operation failed with reason:\n\n{:erpc, :timeout}"

assert_raise Nebulex.RPCError, msg, fn ->
Partitioned.get(1, timeout: 0)
end
end

test "runtime error" do
_ = Process.flag(:trap_exit, true)

Expand Down
7 changes: 5 additions & 2 deletions test/support/test_cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ defmodule Nebulex.TestCache do

@impl true
def get(_, key, _) do
if is_integer(key), do: raise(ArgumentError, "Error")
:ok
if is_integer(key) do
raise ArgumentError, "Error"
else
:ok
end
end

@impl true
Expand Down

0 comments on commit 998a30f

Please sign in to comment.