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 ignoring certain exceptions #55

Merged
merged 1 commit into from
Sep 10, 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
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 @@ -22,7 +22,7 @@
test "reports arithmetic error" do
capture_log(fn ->
in_unlinked_process(fn ->
1 / 0

Check warning on line 25 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 25 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 25 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 25 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 25 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 25 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)
end)

Expand Down Expand Up @@ -311,7 +311,7 @@
test "reports Exception manually" do
in_unlinked_process(fn ->
try do
1 / 0

Check warning on line 314 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 314 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 314 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 314 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 314 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 314 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
catch
kind, reason ->
Tower.handle_caught(kind, reason, __STACKTRACE__)
Expand All @@ -337,7 +337,7 @@
test "reports Exception manually (shorthand)" do
in_unlinked_process(fn ->
try do
1 / 0

Check warning on line 340 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 340 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 340 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 340 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 340 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 340 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
rescue
e ->
Tower.handle_exception(e, __STACKTRACE__)
Expand Down Expand Up @@ -515,7 +515,7 @@

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

Check warning on line 518 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 518 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 518 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 518 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 518 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 518 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)
end)

Expand Down Expand Up @@ -556,7 +556,7 @@
capture_log(fn ->
for _ <- 1..2 do
in_unlinked_process(fn ->
1 / 0

Check warning on line 559 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 559 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 559 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 559 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 559 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 559 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)
end

Expand Down Expand Up @@ -591,6 +591,29 @@
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
Loading