Skip to content

Commit

Permalink
feat: supports ignoring certain exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Sep 10, 2024
1 parent 3517aac commit 9b63e62
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/tower.ex
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,10 @@ defmodule Tower do
@spec handle_exception(Exception.t(), Exception.stacktrace(), Keyword.t()) :: :ok
def handle_exception(exception, stacktrace, options \\ [])
when is_exception(exception) and is_list(stacktrace) do
Event.from_exception(exception, stacktrace, options)
|> report_event()
unless exception.__struct__ in ignored_exceptions() do
Event.from_exception(exception, stacktrace, options)
|> report_event()
end
end

@doc """
Expand Down Expand Up @@ -455,4 +457,8 @@ defmodule Tower do
Tower.TaskSupervisor
|> Task.Supervisor.start_child(fun)
end

defp ignored_exceptions do
Application.fetch_env!(:tower, :ignored_exceptions)
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ defmodule Tower.MixProject do
extra_applications: [:logger, :inets],
env: [
reporters: [Tower.EphemeralReporter],
log_level: :critical
log_level: :critical,
ignored_exceptions: []
],
mod: {Tower.Application, []}
]
Expand Down
23 changes: 23 additions & 0 deletions test/tower_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,29 @@ defmodule TowerTest do
assert similarity_id != other_similarity_id
end

test "doesn't report ignored exceptions" do
put_env(:ignored_exceptions, [ArithmeticError])

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

Check warning on line 599 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 599 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 599 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 599 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 599 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 599 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError
end)

in_unlinked_process(fn ->
raise "error"
end)
end)

assert_eventually(
[
%{
kind: :error,
reason: %RuntimeError{message: "error"}
}
] = reported_events()
)
end

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

Expand Down

0 comments on commit 9b63e62

Please sign in to comment.