Skip to content

Commit

Permalink
reports throw
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Sep 4, 2024
1 parent fbf1388 commit e03237b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
20 changes: 20 additions & 0 deletions lib/tower_sentry/reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ defmodule TowerSentry.Reporter do
end
end

def report_event(%Tower.Event{
kind: :throw,
reason: reason,
stacktrace: stacktrace,
id: id,
metadata: metadata
}) do
if enabled?() do
# TODO: Include plug conn data if available
Sentry.capture_message(
"(throw) #{reason}",
stacktrace: stacktrace,
level: :error,
extra: %{id: id, metadata: metadata}
)
else
IO.puts("Tower.Sentry NOT enabled, ignoring...")
end
end

def report_event(%Tower.Event{
kind: :message,
level: level,
Expand Down
59 changes: 55 additions & 4 deletions test/tower_sentry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule TowerSentryTest do
{:ok, bypass: bypass}
end

test "reports arithmetic error when a Plug.Conn NOT present", %{bypass: bypass} do
test "reports arithmetic error", %{bypass: bypass} do
# ref message synchronization trick copied from
# https://github.com/PSPDFKit-labs/bypass/issues/112
parent = self()
Expand Down Expand Up @@ -53,10 +53,9 @@ defmodule TowerSentryTest do

assert(
%{
"function" =>
~s(anonymous fn/0 in TowerSentryTest."test reports arithmetic error when a Plug.Conn NOT present"/1),
"function" => ~s(anonymous fn/0 in TowerSentryTest."test reports arithmetic error"/1),
"filename" => "test/tower_sentry_test.exs",
"lineno" => 72
"lineno" => 71
} = List.last(frames)
)

Expand All @@ -76,6 +75,58 @@ defmodule TowerSentryTest do
assert_receive({^ref, :sent}, 500)
end

test "reports throw", %{bypass: bypass} do
# ref message synchronization trick copied from
# https://github.com/PSPDFKit-labs/bypass/issues/112
parent = self()
ref = make_ref()

Bypass.expect_once(bypass, "POST", "/api/1/envelope", fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)

assert [_id, _header, event] = String.split(body, "\n", trim: true)

assert(
{
:ok,
%{
"level" => "error",
"environment" => "test",
"exception" => [],
"message" => %{
"formatted" => "(throw) something"
},
"threads" => [thread],
}
} = Jason.decode(event)
)

assert %{"stacktrace" => %{"frames" => frames}} = thread

assert(
%{
"function" => ~s(anonymous fn/0 in TowerSentryTest."test reports throw"/1),
"filename" => "test/tower_sentry_test.exs",
"lineno" => 123
} = List.last(frames)
)

send(parent, {ref, :sent})

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

capture_log(fn ->
in_unlinked_process(fn ->
throw("something")
end)
end)

assert_receive({^ref, :sent}, 500)
end

test "reports arithmetic error when a Plug.Conn IS present with Plug.Cowboy", %{bypass: bypass} do
# ref message synchronization trick copied from
# https://github.com/PSPDFKit-labs/bypass/issues/112
Expand Down

0 comments on commit e03237b

Please sign in to comment.