Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supports reporting Tower messages as BugSnag exceptions with proper severity set #11

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/tower_bugsnag/bugsnag/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ defmodule TowerBugsnag.Bugsnag.Event do
}
end

def from_tower_event(
%Tower.Event{
level: level,
kind: :message,
reason: message,
stacktrace: stacktrace,
plug_conn: plug_conn
} = event
) do
%{
exceptions: [
%{
errorClass: inspect(message),
message: inspect(message),
stacktrace: stacktrace_entries(stacktrace)
}
],
unhandled: !manual_report?(event),
severity: severity_from_tower_level(level),
app: app_data(),
request: request_data(plug_conn)
}
end

defp stacktrace_entries(nil) do
[]
end

defp stacktrace_entries(stacktrace) do
stacktrace
|> Enum.map(&stacktrace_entry(&1))
Expand Down Expand Up @@ -110,6 +138,14 @@ defmodule TowerBugsnag.Bugsnag.Event do
Exception.format_mfa(m, f, length(args))
end

defp severity_from_tower_level(level) when level in [:warning, :info] do
level
end

defp severity_from_tower_level(_) do
:error
end

defp app_data do
%{
releaseStage: release_stage()
Expand Down
8 changes: 0 additions & 8 deletions lib/tower_bugsnag/reporter.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
defmodule TowerBugsnag.Reporter do
def report_event(%Tower.Event{kind: :message}) do
if enabled?() do
IO.puts("Bugsnag DOES NOT support reporting messages, ignoring...")
else
IO.puts("TowerBugsnag NOT enabled, ignoring...")
end
end

def report_event(%Tower.Event{} = tower_event) do
if enabled?() do
tower_event
Expand Down
40 changes: 40 additions & 0 deletions test/tower_bugsnag_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

capture_log(fn ->
in_unlinked_process(fn ->
1 / 0

Check warning on line 69 in test/tower_bugsnag_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.14)

the call to //2 will fail with ArithmeticError

Check warning on line 69 in test/tower_bugsnag_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.14)

the call to //2 will fail with ArithmeticError

Check warning on line 69 in test/tower_bugsnag_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.14)

the call to //2 will fail with ArithmeticError
end)
end)
end)
Expand Down Expand Up @@ -392,6 +392,46 @@
end)
end

test "reports message", %{bypass: bypass} do
waiting_for(fn done ->
Bypass.expect_once(bypass, "POST", "/", fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)

assert(
{
:ok,
%{
"events" => [
%{
"exceptions" => [
%{
"errorClass" => "\"something interesting happened\"",
"message" => "\"something interesting happened\"",
"stacktrace" => []
}
],
"severity" => "info",
"app" => %{
"releaseStage" => "test"
},
"unhandled" => false
}
]
}
} = Jason.decode(body)
)

done.()

conn
|> Plug.Conn.put_resp_content_type("application/json")
|> Plug.Conn.resp(200, Jason.encode!(%{"ok" => true}))
end)

Tower.report_message(:info, "something interesting happened")
end)
end

defp waiting_for(fun) do
# ref message synchronization trick copied from
# https://github.com/PSPDFKit-labs/bypass/issues/112
Expand Down
Loading