Skip to content

Commit 5729824

Browse files
authored
Do not fingerprint error reason (#19)
Including the error reason among the fingerprinted parameters caused wrong error grouping in certain scenarios such as accessing not-existing map fields. This pull request stores only the original reason in the error itself and then records each occurrence reason. This ensures that errors are grouped together even though the reason has slight changes. I am not sure if it is worth to keep the reason in the error itself, but we can always remove it later so this doesn't have to be decided now.
1 parent 4ba3283 commit 5729824

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

lib/error_tracker.ex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@ defmodule ErrorTracker do
1212
alias ErrorTracker.Repo
1313

1414
def report(exception, stacktrace, given_context \\ %{}) do
15+
{kind, reason} =
16+
case exception do
17+
%struct{} = ex when is_exception(ex) ->
18+
{to_string(struct), Exception.message(ex)}
19+
20+
{_kind, %struct{} = ex} when is_exception(ex) ->
21+
{to_string(struct), Exception.message(ex)}
22+
23+
{kind, ex} ->
24+
{to_string(kind), to_string(ex)}
25+
end
26+
1527
{:ok, stacktrace} = ErrorTracker.Stacktrace.new(stacktrace)
16-
{:ok, error} = Error.new(exception, stacktrace)
28+
{:ok, error} = Error.new(kind, reason, stacktrace)
1729

1830
context = Map.merge(get_context(), given_context)
1931

@@ -24,7 +36,7 @@ defmodule ErrorTracker do
2436
)
2537

2638
error
27-
|> Ecto.build_assoc(:occurrences, stacktrace: stacktrace, context: context)
39+
|> Ecto.build_assoc(:occurrences, stacktrace: stacktrace, context: context, reason: reason)
2840
|> Repo.insert!()
2941
end
3042

lib/error_tracker/migrations/postgres/v01.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ defmodule ErrorTracker.Migrations.Postgres.V01 do
2222

2323
create table(:error_tracker_occurrences, prefix: prefix) do
2424
add :context, :map, null: false
25+
add :reason, :text, null: false
2526
add :stacktrace, :map, null: false
2627
add :error_id, references(:error_tracker_errors, on_delete: :delete_all), null: false
2728

lib/error_tracker/schemas/error.ex

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,11 @@ defmodule ErrorTracker.Error do
2222
timestamps(type: :utc_datetime_usec)
2323
end
2424

25-
def new(exception, stacktrace = %ErrorTracker.Stacktrace{}) do
25+
def new(kind, reason, stacktrace = %ErrorTracker.Stacktrace{}) do
2626
source = ErrorTracker.Stacktrace.source(stacktrace)
2727

28-
{kind, reason} =
29-
case exception do
30-
%struct{} = ex when is_exception(ex) ->
31-
{to_string(struct), Exception.message(ex)}
32-
33-
{_kind, %struct{} = ex} when is_exception(ex) ->
34-
{to_string(struct), Exception.message(ex)}
35-
36-
{kind, ex} ->
37-
{to_string(kind), to_string(ex)}
38-
end
39-
4028
params = [
4129
kind: to_string(kind),
42-
reason: reason,
4330
source_line: "#{source.file}:#{source.line}",
4431
source_function: "#{source.module}.#{source.function}/#{source.arity}"
4532
]
@@ -48,6 +35,7 @@ defmodule ErrorTracker.Error do
4835

4936
%__MODULE__{}
5037
|> Ecto.Changeset.change(params)
38+
|> Ecto.Changeset.put_change(:reason, reason)
5139
|> Ecto.Changeset.put_change(:fingerprint, Base.encode16(fingerprint))
5240
|> Ecto.Changeset.put_change(:last_occurrence_at, DateTime.utc_now())
5341
|> Ecto.Changeset.apply_action(:new)

lib/error_tracker/schemas/occurrence.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule ErrorTracker.Occurrence do
1010

1111
schema "error_tracker_occurrences" do
1212
field :context, :map
13+
field :reason, :string
1314

1415
embeds_one :stacktrace, ErrorTracker.Stacktrace
1516
belongs_to :error, ErrorTracker.Error

lib/error_tracker/web/live/show.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<div class="grid grid-cols-1 md:grid-cols-4 md:space-x-3 mt-6 gap-2">
1717
<div class="px-3 md:col-span-3 md:border-r-2 md:border-gray-500 space-y-8">
1818
<.section title="Full message">
19-
<pre class="overflow-auto p-4 bg-gray-600"><%= @error.reason %></pre>
19+
<pre class="overflow-auto p-4 bg-gray-600"><%= @occurrence.reason %></pre>
2020
</.section>
2121

2222
<.section title="Source">

0 commit comments

Comments
 (0)