From 5e3535f4774ab88964f1067e44d43ec98b71966c Mon Sep 17 00:00:00 2001 From: Gonzalo <456459+grzuy@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:19:42 -0300 Subject: [PATCH] feat: reports exception stacktrace frames (#6) --- lib/tower_rollbar/rollbar/item.ex | 24 +++++++++++++++++++++--- test/tower_rollbar/rollbar/item_test.exs | 23 ++++++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/tower_rollbar/rollbar/item.ex b/lib/tower_rollbar/rollbar/item.ex index 5719cdd..676b82b 100644 --- a/lib/tower_rollbar/rollbar/item.ex +++ b/lib/tower_rollbar/rollbar/item.ex @@ -98,9 +98,27 @@ defmodule TowerRollbar.Rollbar.Item do } end - defp frames(_stacktrace) do - # TODO: implement - [] + defp frames(stacktrace) do + stacktrace + |> Enum.map(fn {m, f, a, location} -> + frame = %{ + "method" => Exception.format_mfa(m, f, a) + } + + frame = + if location[:file] do + Map.put(frame, "filename", to_string(location[:file])) + else + frame + end + + if location[:line] do + Map.put(frame, "lineno", location[:line]) + else + frame + end + end) + |> Enum.reverse() end defp environment do diff --git a/test/tower_rollbar/rollbar/item_test.exs b/test/tower_rollbar/rollbar/item_test.exs index 6842661..1c4b9f8 100644 --- a/test/tower_rollbar/rollbar/item_test.exs +++ b/test/tower_rollbar/rollbar/item_test.exs @@ -22,7 +22,28 @@ defmodule TowerRollbar.Rollbar.ItemTest do "level" => "error", "body" => %{ "trace" => %{ - "frames" => [], + "frames" => [ + %{ + "method" => _, + "filename" => "lib/ex_unit/runner.ex", + "lineno" => _ + }, + %{ + "method" => _, + "filename" => "timer.erl", + "lineno" => _ + }, + %{ + "method" => _, + "filename" => "lib/ex_unit/runner.ex", + "lineno" => _ + }, + %{ + "method" => ~s(TowerRollbar.Rollbar.ItemTest."test from_exception"/1), + "filename" => "test/tower_rollbar/rollbar/item_test.exs", + "lineno" => 12 + } + ], "exception" => %{ "class" => "RuntimeError", "message" => "a test"