From a64fd051dcbb1f3e2878cbb152c91d293c17d98b Mon Sep 17 00:00:00 2001 From: Tom Maszk Date: Tue, 30 Jan 2024 12:04:48 -0500 Subject: [PATCH] parse elixir exceptions --- lib/new_relic/error/metadata_reporter.ex | 16 +++++++++------- test/error_test.exs | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/new_relic/error/metadata_reporter.ex b/lib/new_relic/error/metadata_reporter.ex index 8b09437..b34b1e9 100644 --- a/lib/new_relic/error/metadata_reporter.ex +++ b/lib/new_relic/error/metadata_reporter.ex @@ -62,19 +62,21 @@ defmodule NewRelic.Error.MetadataReporter do end end - defp parse_reason({exception, stacktrace}) do - type = parse_type(exception) + defp parse_reason({%type{message: message} = exception, stacktrace}) do expected = parse_error_expected(exception) - message = Map.get(exception, :message) + type = inspect(type) reason = "(#{type}) #{message}" {type, reason, stacktrace, expected} end - defp parse_type(%type{}) do - type - |> Module.split() - |> Enum.join(".") + defp parse_reason({exception, stacktrace}) do + exception = Exception.normalize(:error, exception, stacktrace) + type = inspect(exception.__struct__) + message = Exception.message(exception) + reason = "(#{type}) #{message}" + + {type, reason, stacktrace, false} end defp parse_process_name([], [{module, _f, _a, _} | _]), do: inspect(module) diff --git a/test/error_test.exs b/test/error_test.exs index f325f9c..0642ce1 100644 --- a/test/error_test.exs +++ b/test/error_test.exs @@ -186,4 +186,20 @@ defmodule ErrorTest do intrinsic[:"error.class"] == "File.Error" end) end + + test "Catch a function clause error inside a Task" do + TestHelper.restart_harvest_cycle(Collector.TransactionErrorEvent.HarvestCycle) + + :proc_lib.spawn(fn -> + Task.async(fn -> ErrorDummy.handle_call(:foo, nil, nil) end) |> Task.await() + end) + + :timer.sleep(100) + + events = TestHelper.gather_harvest(Collector.TransactionErrorEvent.Harvester) + + assert Enum.find(events, fn [intrinsic, _, _] -> + intrinsic[:"error.class"] == "FunctionClauseError" + end) + end end