Skip to content

Return errors when an event cannot be emitted #12

@benwilson512

Description

@benwilson512

Hey @LostKobrakai, shamefully I'm only now upgrading our main app to take advantage of some of the changes you made last year. In the transition from Repo.serial to Events.emit I'm finding it difficult to return errors to the user, and I'm curious what you're doing.

For example today I have code like this:

def edit_comment(shipment, comment_data) do
  Repo.serial(shipment, fn shipment ->
    if comment = find_comment(comment_data) do
       Events.emit(shipment, %CommentEdited{...}) # etc
    else
      {:error, "comment not found"}
    end
  end)
end

It isn't clear how I can get the same functionality anymore. Specifically I need two things:
A) Blocking serialization around the shipment for the entire duration of the logic flow so that I don't need to worry about serialization errors on commit
B) The ability to return non event types

The api around Events.emit is slick, but as far as I can tell, the return type of the function you pass to Events.emit/3 is event.t | [event.t]. so it isn't a drop in replacement for Repo.serial. I could do:

def edit_comment(shipment, comment_data) do
  if comment = find_comment(comment_data) do
     Events.emit(shipment, fn shipment -> %CommentEdited{...} end) # etc
  else
    {:error, "comment not found"}
  end
end

However if I do this I've lost serialization guarantees.

Obviously I can add a Repo.serial back to my own Repo module, but I'm struggling to see how Fable can be used properly without it.

Notably, this isn't mitigated by the use of Multi, the same issues persist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions