Skip to content

Commit c1f9fba

Browse files
committed
Rollback error upsert if occurrence cannot be stored.
1 parent 657f753 commit c1f9fba

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

lib/error_tracker.ex

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,25 @@ defmodule ErrorTracker do
188188
existing_status =
189189
Repo.one(from e in Error, where: [fingerprint: ^error.fingerprint], select: e.status)
190190

191-
error =
192-
Repo.insert!(error,
193-
on_conflict: [set: [status: :unresolved, last_occurrence_at: DateTime.utc_now()]],
194-
conflict_target: :fingerprint
195-
)
196-
197-
occurrence =
198-
error
199-
|> Ecto.build_assoc(:occurrences, stacktrace: stacktrace, context: context, reason: reason)
200-
|> Repo.insert!()
191+
{:ok, {error, occurrence}} =
192+
Repo.transaction(fn ->
193+
error =
194+
Repo.insert!(error,
195+
on_conflict: [set: [status: :unresolved, last_occurrence_at: DateTime.utc_now()]],
196+
conflict_target: :fingerprint
197+
)
198+
199+
occurrence =
200+
error
201+
|> Ecto.build_assoc(:occurrences,
202+
stacktrace: stacktrace,
203+
context: context,
204+
reason: reason
205+
)
206+
|> Repo.insert!()
207+
208+
{error, occurrence}
209+
end)
201210

202211
# If the error existed and was marked as resolved before this exception,
203212
# sent a Telemetry event

lib/error_tracker/repo.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ defmodule ErrorTracker.Repo do
2929
dispatch(:aggregate, [queryable, aggregate], opts)
3030
end
3131

32+
def transaction(fun_or_multi, opts \\ []) do
33+
dispatch(:transaction, [fun_or_multi], opts)
34+
end
35+
3236
def __adapter__, do: repo().__adapter__()
3337

3438
defp dispatch(action, args, opts) do

0 commit comments

Comments
 (0)