I have a form which collects data and that data is inserted into multiple db tables. If one of those operations fails, whole transaction should fail and none of them should be inserted into a db so I’m using Ecto.Multi.
Now, if one of the operations fails, Repo.transaction() sends an error and that error is shown to the user. That all works fine but how can I get errors if more then one operation failed?
Here’s a simplified code, without associations to make it clearer.
Ecto.Multi.new() |> Ecto.Multi.insert(:album, album_changeset) |> Ecto.Multi.insert(:track, track_changeset) |> Ecto.Multi.insert(:artist, artist_changeset) |> Repo,transaction()
If there is something wrong when inserting album, transaction would fail, user would be returned to the form and there would be an error in the album field inside the form but if there was also an error when inserting track or album, those errors would not get shown at all, only the error from the first failed transaction gets shown so user could think everything is fine with those fields when in fact it’s not.
So, how can I collect and show errors from all failed transactions to the user?