Repository: elixir-error-tracker/error-tracker
Pull request #106 (2024-11-07 16:00:36)

Store Ash bread crumbs in occurrence context
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.

To test this you can clone the [Ash Realworld](https://github.com/team-alembic/realworld) example project and update the ash dependency to main with `{:ash, github: "ash-project/ash", override: true}`. Here is a screenshot of how it looks like:

![image](https://github.com/user-attachments/assets/103d99c6-bb43-4c74-8df8-4b210cca3bd9)



Patch:
diff --git a/lib/error_tracker.ex b/lib/error_tracker.ex
index 09f176b..0c5d09f 100644
--- a/lib/error_tracker.ex
+++ b/lib/error_tracker.ex
@@ -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)
@@ -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)


Test patch:
diff --git a/test/error_tracker_test.exs b/test/error_tracker_test.exs
index 8d9ea79..aacf7b2 100644
--- a/test/error_tracker_test.exs
+++ b/test/error_tracker_test.exs
@@ -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
@@ -119,3 +130,7 @@ defmodule ErrorTrackerTest do
     end
   end
 end
+
+defmodule ErrorWithBreadcrumbs do
+  defexception [:message, :bread_crumbs]
+end