Skip to content

Commit

Permalink
adapt to separate throw/exit/message
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Jun 20, 2024
1 parent 155973f commit 881e95f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
21 changes: 17 additions & 4 deletions lib/tower_rollbar/reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,35 @@ defmodule TowerRollbar.Reporter do
end

@impl true
def report_term(reason, metadata \\ %{}) do
def report_throw(reason, stacktrace, metadata \\ %{}) when is_list(stacktrace) do
if enabled?() do
Rollbar.Client.post(
"/item",
Rollbar.Item.from_term(reason, level: Map.get(metadata, :level))
Rollbar.Item.from_throw(reason, stacktrace, plug_conn: plug_conn(metadata))
)
else
IO.puts("Tower.Rollbar NOT enabled, ignoring...")
end
end

def report_message(message, options \\ []) when is_binary(message) do
@impl true
def report_exit(reason, stacktrace, metadata \\ %{}) when is_list(stacktrace) do
if enabled?() do
Rollbar.Client.post(
"/item",
Rollbar.Item.from_exit(reason, stacktrace, plug_conn: plug_conn(metadata))
)
else
IO.puts("Tower.Rollbar NOT enabled, ignoring...")
end
end

@impl true
def report_message(level, message, _metadata \\ %{}) when is_binary(message) do
if enabled?() do
Rollbar.Client.post(
"/item",
Rollbar.Item.from_message(message, options)
Rollbar.Item.from_message(message, level: level)
)
else
IO.puts("Tower.Rollbar NOT enabled, ignoring...")
Expand Down
35 changes: 19 additions & 16 deletions lib/tower_rollbar/rollbar/item.ex
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
defmodule TowerRollbar.Rollbar.Item do
def from_exception(exception, stacktrace, options \\ [])
when is_exception(exception) and is_list(stacktrace) do
%{
"trace" => %{
"frames" => frames(stacktrace),
"exception" => %{
"class" => inspect(exception.__struct__),
"message" => Exception.message(exception)
}
}
}
|> item_from_body(Keyword.merge([level: :error], options))
trace(inspect(exception.__struct__), Exception.message(exception), stacktrace, options)
end

def from_throw(reason, stacktrace, options \\ []) when is_list(stacktrace) do
trace("uncaught throw", reason, stacktrace, options)
end

def from_exit(reason, stacktrace, options \\ []) when is_list(stacktrace) do
trace("exit", reason, stacktrace, options)
end

def from_term(reason, options \\ []) do
def from_message(message, options \\ []) when is_binary(message) do
%{
"message" => %{
"body" => inspect(reason)
"body" => message
}
}
|> item_from_body(Keyword.merge([level: :info], options))
end

def from_message(message, options \\ []) when is_binary(message) do
defp trace(class, reason, stacktrace, options) do
%{
"message" => %{
"body" => message
"trace" => %{
"frames" => frames(stacktrace),
"exception" => %{
"class" => class,
"message" => reason
}
}
}
|> item_from_body(Keyword.merge([level: :info], options))
|> item_from_body(Keyword.merge([level: :error], options))
end

defp item_from_body(body, options) when is_map(body) do
Expand Down

0 comments on commit 881e95f

Please sign in to comment.