From b1f7e0b9bcae261cc75397649859089566f2f037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Tue, 6 Aug 2024 19:20:21 +0200 Subject: [PATCH 1/6] Parse arity on some Erlang exceptions --- lib/error_tracker/schemas/stacktrace.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/error_tracker/schemas/stacktrace.ex b/lib/error_tracker/schemas/stacktrace.ex index 14f3517..cf22f31 100644 --- a/lib/error_tracker/schemas/stacktrace.ex +++ b/lib/error_tracker/schemas/stacktrace.ex @@ -27,7 +27,7 @@ defmodule ErrorTracker.Stacktrace do application: to_string(application), module: module |> to_string() |> String.replace_prefix("Elixir.", ""), function: to_string(function), - arity: arity, + arity: normalize_arity(arity), file: to_string(opts[:file]), line: opts[:line] } @@ -39,6 +39,9 @@ defmodule ErrorTracker.Stacktrace do |> Ecto.Changeset.apply_action(:new) end + defp normalize_arity(a) when is_integer(a), do: a + defp normalize_arity(a) when is_list(a), do: length(a) + defp line_changeset(line = %__MODULE__.Line{}, params) do Ecto.Changeset.cast(line, params, ~w[application module function arity file line]a) end From 11e093062f7fad520627ca99b71ff84f66333930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Tue, 6 Aug 2024 19:42:30 +0200 Subject: [PATCH 2/6] Recognize when there is no file information --- lib/error_tracker/schemas/error.ex | 3 ++- lib/error_tracker/web/live/show.html.heex | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/error_tracker/schemas/error.ex b/lib/error_tracker/schemas/error.ex index fb3808a..26265d4 100644 --- a/lib/error_tracker/schemas/error.ex +++ b/lib/error_tracker/schemas/error.ex @@ -29,10 +29,11 @@ defmodule ErrorTracker.Error do @doc false def new(kind, reason, stacktrace = %ErrorTracker.Stacktrace{}) do source = ErrorTracker.Stacktrace.source(stacktrace) + source_line = if source.file, do: "#{source.file}:#{source.line}", else: "nofile" params = [ kind: to_string(kind), - source_line: "#{source.file}:#{source.line}", + source_line: source_line, source_function: "#{source.module}.#{source.function}/#{source.arity}" ] diff --git a/lib/error_tracker/web/live/show.html.heex b/lib/error_tracker/web/live/show.html.heex index d93d199..ebf974b 100644 --- a/lib/error_tracker/web/live/show.html.heex +++ b/lib/error_tracker/web/live/show.html.heex @@ -48,7 +48,7 @@
(<%= line.application || @app %>)
<%= "#{sanitize_module(line.module)}.#{line.function}/#{line.arity}" %>
-                <%= "#{line.file}.#{line.line}" %>
+ <%= if line.line, do: "#{line.file}:#{line.line}", else: "nofile" %> From d9eb01d03ffe240a561d29d9f1d4616d84289f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Tue, 6 Aug 2024 19:42:42 +0200 Subject: [PATCH 3/6] Normalize Erlang exception --- lib/error_tracker.ex | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/error_tracker.ex b/lib/error_tracker.ex index f05a51b..b647da3 100644 --- a/lib/error_tracker.ex +++ b/lib/error_tracker.ex @@ -102,18 +102,9 @@ defmodule ErrorTracker do messages. """ 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 + IO.inspect(exception) + {kind, reason} = normalize_exception(exception, stacktrace) {:ok, stacktrace} = ErrorTracker.Stacktrace.new(stacktrace) {:ok, error} = Error.new(kind, reason, stacktrace) @@ -181,4 +172,18 @@ defmodule ErrorTracker do def get_context do Process.get(:error_tracker_context, %{}) end + + defp normalize_exception(%struct{} = ex, _stacktrace) when is_exception(ex) do + {to_string(struct), Exception.message(ex)} + end + + defp normalize_exception({kind, ex}, stacktrace) do + case Exception.normalize(kind, ex, stacktrace) do + %struct{} -> + {to_string(struct), Exception.message(ex)} + + other -> + {to_string(kind), to_string(other)} + end + end end From 4ca69de49f0ad8210429803303e0c05782c2feaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Tue, 6 Aug 2024 19:49:37 +0200 Subject: [PATCH 4/6] Remove inspect --- lib/error_tracker.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/error_tracker.ex b/lib/error_tracker.ex index b647da3..b807473 100644 --- a/lib/error_tracker.ex +++ b/lib/error_tracker.ex @@ -102,8 +102,6 @@ defmodule ErrorTracker do messages. """ def report(exception, stacktrace, given_context \\ %{}) do - IO.inspect(exception) - {kind, reason} = normalize_exception(exception, stacktrace) {:ok, stacktrace} = ErrorTracker.Stacktrace.new(stacktrace) {:ok, error} = Error.new(kind, reason, stacktrace) From 145c68da8e08b5e5956fb87f6709e7fa34b9fad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Tue, 6 Aug 2024 19:50:19 +0200 Subject: [PATCH 5/6] Update documentation --- lib/error_tracker.ex | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/error_tracker.ex b/lib/error_tracker.ex index b807473..2ea2c7e 100644 --- a/lib/error_tracker.ex +++ b/lib/error_tracker.ex @@ -94,12 +94,8 @@ defmodule ErrorTracker do * An exception struct: the module of the exception is stored along with the exception message. - * A `{kind, exception}` tuple in which the `exception` is a struct: it - behaves the same as when passing just the exception struct. - - * A `{kind, reason}` tuple: it stores the kind and the message itself cast - to strings, which is useful for some errors like EXIT signals or custom error - messages. + * A `{kind, exception}` tuple in which case the information is converted to + an Elixir exception (if possible) and stored. """ def report(exception, stacktrace, given_context \\ %{}) do {kind, reason} = normalize_exception(exception, stacktrace) From 28c6c2b453ee2325521d68bfa79bd952c538d966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Wed, 7 Aug 2024 09:05:17 +0200 Subject: [PATCH 6/6] Fix message extraction from normalized exceptions --- lib/error_tracker.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/error_tracker.ex b/lib/error_tracker.ex index 2ea2c7e..327ce86 100644 --- a/lib/error_tracker.ex +++ b/lib/error_tracker.ex @@ -173,7 +173,7 @@ defmodule ErrorTracker do defp normalize_exception({kind, ex}, stacktrace) do case Exception.normalize(kind, ex, stacktrace) do - %struct{} -> + %struct{} = ex -> {to_string(struct), Exception.message(ex)} other ->