Skip to content

Commit

Permalink
Store bread crumbs in error context
Browse files Browse the repository at this point in the history
Some frameworks such as Ash provide bread crumbs in their errors. This
commit records those breadcrumbs and stores them in the occurrence
context.
  • Loading branch information
crbelaus committed Nov 7, 2024
1 parent d7bc886 commit 76661f0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/error_tracker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ defmodule ErrorTracker do
{:ok, error} = Error.new(kind, reason, stacktrace)
context = Map.merge(get_context(), given_context)

context =
if bread_crumbs = bread_crumbs(exception),
do: Map.put(context, "bread_crumbs", bread_crumbs),
else: context

if enabled?() && !ignored?(error, context) do
sanitized_context = sanitize_context(context)
{_error, occurrence} = upsert_error!(error, stacktrace, sanitized_context, reason)
Expand Down Expand Up @@ -232,6 +237,14 @@ defmodule ErrorTracker do
end
end

defp bread_crumbs(exception) do
case exception do
{_kind, exception} -> bread_crumbs(exception)
%{bread_crumbs: bread_crumbs} -> bread_crumbs
_other -> nil
end
end

defp upsert_error!(error, stacktrace, context, reason) do
existing_status =
Repo.one(from e in Error, where: [fingerprint: ^error.fingerprint], select: e.status)
Expand Down
15 changes: 15 additions & 0 deletions test/error_tracker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ defmodule ErrorTrackerTest do
test "with enabled flag to false it does not store the exception" do
assert report_error(fn -> raise "Sample error" end) == :noop
end

test "includes bread crumbs in the context if present" do
bread_crumbs = ["bread crumb 1", "bread crumb 2"]

occurrence =
report_error(fn ->
raise ErrorWithBreadcrumbs, message: "test", bread_crumbs: bread_crumbs
end)

assert occurrence.context["bread_crumbs"] == bread_crumbs
end
end

describe inspect(&ErrorTracker.resolve/1) do
Expand All @@ -119,3 +130,7 @@ defmodule ErrorTrackerTest do
end
end
end

defmodule ErrorWithBreadcrumbs do
defexception [:message, :bread_crumbs]
end

0 comments on commit 76661f0

Please sign in to comment.