From 243de7d1757d1f8dd776eb115f017d0aba15ff08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Fri, 28 Feb 2025 17:29:56 +0100 Subject: [PATCH 1/5] Fix error grouping issue The errors are being grouped in a wrong way because a type mismatch comparison was done between the application name and the stacktrace application name. The fix is to convert the configured application name to a string and be able to compare it with the stacktrace application name. --- lib/error_tracker/schemas/stacktrace.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/error_tracker/schemas/stacktrace.ex b/lib/error_tracker/schemas/stacktrace.ex index 24e8bb0..eb3162d 100644 --- a/lib/error_tracker/schemas/stacktrace.ex +++ b/lib/error_tracker/schemas/stacktrace.ex @@ -55,7 +55,7 @@ defmodule ErrorTracker.Stacktrace do application, just the first line. """ def source(stack = %__MODULE__{}) do - client_app = Application.fetch_env!(:error_tracker, :otp_app) + client_app = Application.fetch_env!(:error_tracker, :otp_app) |> to_string() Enum.find(stack.lines, &(&1.application == client_app)) || List.first(stack.lines) end From 207b8fde0a8242482c027da293fa36de7d2b613a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Fri, 28 Feb 2025 17:51:25 +0100 Subject: [PATCH 2/5] Fix tests --- test/error_tracker_test.exs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/error_tracker_test.exs b/test/error_tracker_test.exs index 00538fe..3f48495 100644 --- a/test/error_tracker_test.exs +++ b/test/error_tracker_test.exs @@ -4,7 +4,10 @@ defmodule ErrorTrackerTest do alias ErrorTracker.Error alias ErrorTracker.Occurrence - @relative_file_path Path.relative_to(__ENV__.file, File.cwd!()) + # We use this file path because for some reason the test scripts are not + # handled as part of the application, so the last line of the app executed is + # on the case module. + @relative_file_path "test/support/case.ex" describe inspect(&ErrorTracker.report/3) do setup context do @@ -29,6 +32,12 @@ defmodule ErrorTrackerTest do test "reports badarith errors" do string_var = to_string(1) + # We set the otp_app to `nil` because the error is not reported by an OTP + # application but by this script, so the last line of the stacktrace is not + # the correct one if we leave the right value. + Application.put_env(:error_tracker, :otp_app, nil) + on_exit(fn -> Application.put_env(:error_tracker, :otp_app, :error_tracker) end) + %Occurrence{error: error = %Error{}} = report_error(fn -> 1 + string_var end) From 6dc2ba15e2f2fc8c76def3c39fdc370d17e6ba3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Fri, 28 Feb 2025 18:10:09 +0100 Subject: [PATCH 3/5] Fix tests --- test/error_tracker_test.exs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/test/error_tracker_test.exs b/test/error_tracker_test.exs index 3f48495..6803225 100644 --- a/test/error_tracker_test.exs +++ b/test/error_tracker_test.exs @@ -32,25 +32,17 @@ defmodule ErrorTrackerTest do test "reports badarith errors" do string_var = to_string(1) - # We set the otp_app to `nil` because the error is not reported by an OTP - # application but by this script, so the last line of the stacktrace is not - # the correct one if we leave the right value. - Application.put_env(:error_tracker, :otp_app, nil) - on_exit(fn -> Application.put_env(:error_tracker, :otp_app, :error_tracker) end) - - %Occurrence{error: error = %Error{}} = + %Occurrence{error: error = %Error{}, stacktrace: %{lines: [last_line | _]}} = report_error(fn -> 1 + string_var end) assert error.kind == to_string(ArithmeticError) assert error.reason == "bad argument in arithmetic expression" - # Elixir 1.17.0 reports these errors differently than previous versions - if Version.compare(System.version(), "1.17.0") == :lt do - assert error.source_line =~ @relative_file_path - else - assert error.source_function == "erlang.+/2" - assert error.source_line == "(nofile)" - end + assert last_line.module == "erlang" + assert last_line.function == "+" + assert last_line.arity == 2 + refute last_line.file + refute last_line.line end test "reports undefined function errors" do From ab4b29ef3f82e5ef7414c32b6d141d285ce41c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Fri, 28 Feb 2025 18:27:33 +0100 Subject: [PATCH 4/5] Lets see what happens in CI --- .github/workflows/elixir.yml | 4 ++-- test/error_tracker_test.exs | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index 78fbea3..16c481f 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -83,8 +83,8 @@ jobs: - name: Check application compile warnings run: mix compile --force --warnings-as-errors - - name: Check Credo warnings - run: mix credo + # - name: Check Credo warnings + # run: mix credo - name: Run Tests - SQLite3 run: mix test diff --git a/test/error_tracker_test.exs b/test/error_tracker_test.exs index 6803225..ee056f2 100644 --- a/test/error_tracker_test.exs +++ b/test/error_tracker_test.exs @@ -38,11 +38,21 @@ defmodule ErrorTrackerTest do assert error.kind == to_string(ArithmeticError) assert error.reason == "bad argument in arithmetic expression" - assert last_line.module == "erlang" - assert last_line.function == "+" - assert last_line.arity == 2 - refute last_line.file - refute last_line.line + # Elixir 1.17.0 reports these errors differently than previous versions + if Version.compare(System.version(), "1.17.0") == :lt do + dbg(last_line) + assert last_line.module == "Elixir.ErrorTrackerTest" + assert last_line.function == "report_error/2" + assert last_line.arity == 1 + assert last_line.file == @relative_file_path + assert last_line.line == 11 + else + assert last_line.module == "erlang" + assert last_line.function == "+" + assert last_line.arity == 2 + refute last_line.file + refute last_line.line + end end test "reports undefined function errors" do From 566e57c0ef49371e1b488d0414e706917d7dd63f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Fri, 28 Feb 2025 18:32:52 +0100 Subject: [PATCH 5/5] Fix tests for older versions --- .github/workflows/elixir.yml | 4 ++-- test/error_tracker_test.exs | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index 16c481f..78fbea3 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -83,8 +83,8 @@ jobs: - name: Check application compile warnings run: mix compile --force --warnings-as-errors - # - name: Check Credo warnings - # run: mix credo + - name: Check Credo warnings + run: mix credo - name: Run Tests - SQLite3 run: mix test diff --git a/test/error_tracker_test.exs b/test/error_tracker_test.exs index ee056f2..b5121ee 100644 --- a/test/error_tracker_test.exs +++ b/test/error_tracker_test.exs @@ -40,12 +40,11 @@ defmodule ErrorTrackerTest do # Elixir 1.17.0 reports these errors differently than previous versions if Version.compare(System.version(), "1.17.0") == :lt do - dbg(last_line) - assert last_line.module == "Elixir.ErrorTrackerTest" - assert last_line.function == "report_error/2" + assert last_line.module == "ErrorTrackerTest" + assert last_line.function =~ "&ErrorTracker.report/3 reports badarith errors" assert last_line.arity == 1 - assert last_line.file == @relative_file_path - assert last_line.line == 11 + assert last_line.file + assert last_line.line else assert last_line.module == "erlang" assert last_line.function == "+"