Skip to content

Store Ash bread crumbs in occurrence context #106

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

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading