From 14a5dadeee4229b5660ca86166bfc7ad78b8df6e Mon Sep 17 00:00:00 2001 From: crbelaus Date: Mon, 22 Jul 2024 15:29:44 +0200 Subject: [PATCH] Do not fingerprint error reason --- lib/error_tracker.ex | 16 ++++++++++++++-- lib/error_tracker/migrations/postgres/v01.ex | 1 + lib/error_tracker/schemas/error.ex | 16 ++-------------- lib/error_tracker/schemas/occurrence.ex | 1 + lib/error_tracker/web/live/show.html.heex | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/error_tracker.ex b/lib/error_tracker.ex index 5997214..aa83fb8 100644 --- a/lib/error_tracker.ex +++ b/lib/error_tracker.ex @@ -12,8 +12,20 @@ defmodule ErrorTracker do alias ErrorTracker.Repo def report(exception, stacktrace, given_context \\ %{}) do + {kind, reason} = + case exception do + %struct{} = ex when is_exception(ex) -> + {to_string(struct), Exception.message(ex)} + + {_kind, %struct{} = ex} when is_exception(ex) -> + {to_string(struct), Exception.message(ex)} + + {kind, ex} -> + {to_string(kind), to_string(ex)} + end + {:ok, stacktrace} = ErrorTracker.Stacktrace.new(stacktrace) - {:ok, error} = Error.new(exception, stacktrace) + {:ok, error} = Error.new(kind, reason, stacktrace) context = Map.merge(get_context(), given_context) @@ -24,7 +36,7 @@ defmodule ErrorTracker do ) error - |> Ecto.build_assoc(:occurrences, stacktrace: stacktrace, context: context) + |> Ecto.build_assoc(:occurrences, stacktrace: stacktrace, context: context, reason: reason) |> Repo.insert!() end diff --git a/lib/error_tracker/migrations/postgres/v01.ex b/lib/error_tracker/migrations/postgres/v01.ex index 056bf1f..9946855 100644 --- a/lib/error_tracker/migrations/postgres/v01.ex +++ b/lib/error_tracker/migrations/postgres/v01.ex @@ -22,6 +22,7 @@ defmodule ErrorTracker.Migrations.Postgres.V01 do create table(:error_tracker_occurrences, prefix: prefix) do add :context, :map, null: false + add :reason, :text, null: false add :stacktrace, :map, null: false add :error_id, references(:error_tracker_errors, on_delete: :delete_all), null: false diff --git a/lib/error_tracker/schemas/error.ex b/lib/error_tracker/schemas/error.ex index 443352a..4e1c2e6 100644 --- a/lib/error_tracker/schemas/error.ex +++ b/lib/error_tracker/schemas/error.ex @@ -22,24 +22,11 @@ defmodule ErrorTracker.Error do timestamps(type: :utc_datetime_usec) end - def new(exception, stacktrace = %ErrorTracker.Stacktrace{}) do + def new(kind, reason, stacktrace = %ErrorTracker.Stacktrace{}) do source = ErrorTracker.Stacktrace.source(stacktrace) - {kind, reason} = - case exception do - %struct{} = ex when is_exception(ex) -> - {to_string(struct), Exception.message(ex)} - - {_kind, %struct{} = ex} when is_exception(ex) -> - {to_string(struct), Exception.message(ex)} - - {kind, ex} -> - {to_string(kind), to_string(ex)} - end - params = [ kind: to_string(kind), - reason: reason, source_line: "#{source.file}:#{source.line}", source_function: "#{source.module}.#{source.function}/#{source.arity}" ] @@ -48,6 +35,7 @@ defmodule ErrorTracker.Error do %__MODULE__{} |> Ecto.Changeset.change(params) + |> Ecto.Changeset.put_change(:reason, reason) |> Ecto.Changeset.put_change(:fingerprint, Base.encode16(fingerprint)) |> Ecto.Changeset.put_change(:last_occurrence_at, DateTime.utc_now()) |> Ecto.Changeset.apply_action(:new) diff --git a/lib/error_tracker/schemas/occurrence.ex b/lib/error_tracker/schemas/occurrence.ex index 326c01a..ab76a5c 100644 --- a/lib/error_tracker/schemas/occurrence.ex +++ b/lib/error_tracker/schemas/occurrence.ex @@ -10,6 +10,7 @@ defmodule ErrorTracker.Occurrence do schema "error_tracker_occurrences" do field :context, :map + field :reason, :string embeds_one :stacktrace, ErrorTracker.Stacktrace belongs_to :error, ErrorTracker.Error diff --git a/lib/error_tracker/web/live/show.html.heex b/lib/error_tracker/web/live/show.html.heex index c01f7c4..c0941c6 100644 --- a/lib/error_tracker/web/live/show.html.heex +++ b/lib/error_tracker/web/live/show.html.heex @@ -16,7 +16,7 @@
<.section title="Full message"> -
<%= @error.reason %>
+
<%= @occurrence.reason %>
<.section title="Source">