Skip to content

Commit 428f4c7

Browse files
authored
Store Ash bread crumbs in occurrence context (#106)
Some frameworks such as Ash include breadcrumbs in their errors. This is provided by the fantastic Splode library ([details](https://bsky.app/profile/zachdaniel.dev/post/3l7qsyil4pg2c)). This commit updates the ErrorTracker to detect such bread crumbs and include them in the occurrence context. In the future we may want to revisit this and store them in their own field separate from the context. It is important to note that you don't have to use Ash or Splode to leverage this integration. Breadcrumbs will be automatically extracted from any exception that includes a `bread_crumbs` field. I've discussed this with @zachdaniel and he kindly integrated the breadcrumbs on Ash main for create and change actions.
1 parent 67cde49 commit 428f4c7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/error_tracker.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ defmodule ErrorTracker do
120120
{:ok, error} = Error.new(kind, reason, stacktrace)
121121
context = Map.merge(get_context(), given_context)
122122

123+
context =
124+
if bread_crumbs = bread_crumbs(exception),
125+
do: Map.put(context, "bread_crumbs", bread_crumbs),
126+
else: context
127+
123128
if enabled?() && !ignored?(error, context) do
124129
sanitized_context = sanitize_context(context)
125130
{_error, occurrence} = upsert_error!(error, stacktrace, sanitized_context, reason)
@@ -232,6 +237,14 @@ defmodule ErrorTracker do
232237
end
233238
end
234239

240+
defp bread_crumbs(exception) do
241+
case exception do
242+
{_kind, exception} -> bread_crumbs(exception)
243+
%{bread_crumbs: bread_crumbs} -> bread_crumbs
244+
_other -> nil
245+
end
246+
end
247+
235248
defp upsert_error!(error, stacktrace, context, reason) do
236249
existing_status =
237250
Repo.one(from e in Error, where: [fingerprint: ^error.fingerprint], select: e.status)

test/error_tracker_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ defmodule ErrorTrackerTest do
9999
test "with enabled flag to false it does not store the exception" do
100100
assert report_error(fn -> raise "Sample error" end) == :noop
101101
end
102+
103+
test "includes bread crumbs in the context if present" do
104+
bread_crumbs = ["bread crumb 1", "bread crumb 2"]
105+
106+
occurrence =
107+
report_error(fn ->
108+
raise ErrorWithBreadcrumbs, message: "test", bread_crumbs: bread_crumbs
109+
end)
110+
111+
assert occurrence.context["bread_crumbs"] == bread_crumbs
112+
end
102113
end
103114

104115
describe inspect(&ErrorTracker.resolve/1) do
@@ -119,3 +130,7 @@ defmodule ErrorTrackerTest do
119130
end
120131
end
121132
end
133+
134+
defmodule ErrorWithBreadcrumbs do
135+
defexception [:message, :bread_crumbs]
136+
end

0 commit comments

Comments
 (0)