Skip to content

Commit

Permalink
handle error
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasdarruda committed Jan 24, 2025
1 parent 68fef14 commit 58c4efb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/actors/actor/caller_consumer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -618,23 +618,28 @@ defmodule Actors.Actor.CallerConsumer do
end
end)
rescue
e ->
Logger.error(
"Failure to make a call to actor #{inspect(actor.id.name)} #{inspect(e)}"
)

reraise e, __STACKTRACE__
err ->
Logger.error(Exception.format(:error, err, __STACKTRACE__))

{:error, :actor_invoke, err}
catch
:exit, err ->
# no need to log because this is already logged by the system
{:error, :actor_invoke, err}
end
|> case do
result = :error ->
{:cont, result}

result = {:error, msg} ->
:error = result ->
{:cont, result}

result = {:error, :action_not_found, msg} ->
{:error, :action_not_found, _msg} = result ->
{:halt, result}

{:error, :actor_invoke, error} ->
{:halt, {:error, error}}

{:error, _msg} = result ->
{:cont, result}

result ->
{:halt, result}
end
Expand Down
20 changes: 20 additions & 0 deletions spawn_sdk/spawn_sdk/test/actor/actor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ defmodule Actor.ActorTest do
|> Value.void()
end)

action("test_error", fn _ctx, _payload ->
# match error
1 = 2

Value.of()
end)

action("sum", fn %Context{} = ctx, %MyMessageRequest{id: id, data: data} ->
current_state = ctx.state
new_state = current_state
Expand Down Expand Up @@ -408,6 +415,19 @@ defmodule Actor.ActorTest do
end

describe "invoke with routing" do
test "simple exception error inside an action", ctx do
system = ctx.system

dynamic_actor_name = "#{inspect(make_ref())}" <> "simple_error"

assert {:error, _response} =
SpawnSdk.invoke(dynamic_actor_name,
ref: "my_actor_ref",
system: system,
action: "test_error"
)
end

test "simple call that goes through 3 actors piping each other", ctx do
system = ctx.system

Expand Down

0 comments on commit 58c4efb

Please sign in to comment.