Skip to content

Commit

Permalink
feat: able to manually handle/report error/exit/throw (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy authored Jul 30, 2024
1 parent 6bb230c commit f86ee7a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/tower.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ defmodule Tower do
:ok = Tower.LoggerHandler.detach()
end

def handle_exception(exception, stacktrace, meta)
def handle_exception(exception, stacktrace, meta \\ %{})
when is_exception(exception) and is_list(stacktrace) do
Event.from_exception(exception, stacktrace, meta)
|> report_event()
end

def handle_throw(reason, stacktrace, metadata) do
def handle_throw(reason, stacktrace, metadata \\ %{}) do
Event.from_throw(reason, stacktrace, metadata)
|> report_event()
end

def handle_exit(reason, stacktrace, metadata) do
def handle_exit(reason, stacktrace, metadata \\ %{}) do
Event.from_exit(reason, stacktrace, metadata)
|> report_event()
end
Expand Down
12 changes: 6 additions & 6 deletions lib/tower/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ defmodule Tower.Event do
log_event_meta: :logger.metadata()
}

def from_exception(exception, stacktrace, %{time: time} = log_event_meta) do
def from_exception(exception, stacktrace, log_event_meta) do
%__MODULE__{
time: time,
time: Map.get(log_event_meta, :time, :logger.timestamp()),
level: :error,
kind: :error,
reason: exception,
Expand All @@ -21,9 +21,9 @@ defmodule Tower.Event do
}
end

def from_exit(reason, stacktrace, %{time: time} = log_event_meta) do
def from_exit(reason, stacktrace, log_event_meta) do
%__MODULE__{
time: time,
time: Map.get(log_event_meta, :time, :logger.timestamp()),
level: :error,
kind: :exit,
reason: reason,
Expand All @@ -32,9 +32,9 @@ defmodule Tower.Event do
}
end

def from_throw(reason, stacktrace, %{time: time} = log_event_meta) do
def from_throw(reason, stacktrace, log_event_meta) do
%__MODULE__{
time: time,
time: Map.get(log_event_meta, :time, :logger.timestamp()),
level: :error,
kind: :throw,
reason: reason,
Expand Down
80 changes: 80 additions & 0 deletions test/tower_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,86 @@ defmodule TowerTest do
assert_in_delta(time, :logger.timestamp(), 100_000)
end

test "reports Exception manually" do
in_unlinked_process(fn ->
try do
1 / 0

Check warning on line 269 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 269 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.14, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 269 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.13, 23.3.4.20)

this expression will fail with ArithmeticError
rescue
e ->
Tower.handle_exception(e, __STACKTRACE__)
end
end)

assert_eventually(
[
%{
time: time,
level: :error,
kind: ArithmeticError,
reason: "bad argument in arithmetic expression",
stacktrace: stacktrace
}
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
assert is_list(stacktrace)
end

@tag capture_log: true
test "manually reports a thrown string" do
in_unlinked_process(fn ->
try do
throw("error")
catch
x ->
Tower.handle_throw(x, __STACKTRACE__)
end
end)

assert_eventually(
[
%{
time: time,
level: :error,
kind: :throw,
reason: "error",
stacktrace: stacktrace
}
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
assert is_list(stacktrace)
end

@tag capture_log: true
test "manually reports an abnormal exit" do
in_unlinked_process(fn ->
try do
exit(:abnormal)
catch
:exit, reason ->
Tower.handle_exit(reason, __STACKTRACE__)
end
end)

assert_eventually(
[
%{
time: time,
level: :error,
kind: :exit,
reason: :abnormal,
stacktrace: stacktrace
}
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
assert is_list(stacktrace)
end

defp in_unlinked_process(fun) when is_function(fun, 0) do
{:ok, pid} = Task.Supervisor.start_link()

Expand Down

0 comments on commit f86ee7a

Please sign in to comment.