Skip to content
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

Ensure Ecto projections works with Commanded's event handler error/3 callback #10

Closed
slashdotdash opened this issue Feb 1, 2018 · 8 comments

Comments

@slashdotdash
Copy link
Member

Defining an error/3 callback in a projection, using Commanded's event handler (#133), and returning :skip should ignore the problematic event and allow the projection to continue.

@sardaukar
Copy link

Is there no current way to do error handling?

@slashdotdash
Copy link
Member Author

@sardaukar It needs to be tested using the error/3 callback.

@sardaukar
Copy link

sardaukar commented May 29, 2018

I tried returning an error tuple from project/2 on a projector with error/3 defined and got this on a test for it:

The following arguments were given to Ecto.Repo.Queryable.transaction/4:

         # 1
         Ecto.Adapters.Postgres

         # 2
         Toby.Repo

         # 3
         {:error, :reason}

         # 4
         [timeout: :infinity, pool_timeout: :infinity]

     Attempted function clauses (showing 2 out of 2):

         def transaction(adapter, repo, fun, opts) when is_function(fun, 0)
         def transaction(adapter, repo, %Ecto.Multi{} = multi, opts)

     code: Projector.handle(task_assigned_evt, %{
     stacktrace:
       (ecto) lib/ecto/repo/queryable.ex:15: Ecto.Repo.Queryable.transaction/4
       (toby) lib/projections/ecto.ex:74: Toby.Projectors.Task.AssignedTasks.update_projection/3
       test/toby/projectors/task/assigned_tasks_test.exs:17: (test)

The test:

test "should not project" do
      task_assigned_evt = build(:task_assigned_event)

      last_seen_event_number =
        get_last_seen_event_number("Projectors.Task.AssignedTasks")

      assert :skip =
               Projector.handle(task_assigned_evt, %{
                 event_number: last_seen_event_number + 1
               })
    end

What am I doing wrong?

@slashdotdash
Copy link
Member Author

If you're testing the handle/2 function directly then it should return the error you've defined. This error, the failed event, and a FailureContext struct are then passed to the error/3 function:

alias Commanded.Event.FailureContext

assert {:error, error} = Projector.handle(task_assigned_evt, %{
  event_number: last_seen_event_number + 1
})

assert :skip = Projector.error({:error, error}, task_assigned_evt, %FailureContext{})

@sardaukar
Copy link

I see. Thanks!

@sardaukar
Copy link

Tried again - still get the same error. Is it because I'm calling handle/2 directly, but the projector is running project/2 ?

@sardaukar
Copy link

I also notice the stacktrace mentions update_projection/3

@sardaukar
Copy link

Thanks for supporting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants