Skip to content

Commit

Permalink
pass around a boom module instead of settings
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquinco committed Sep 20, 2024
1 parent 0c8c8bf commit 1be6c5d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
34 changes: 14 additions & 20 deletions lib/boom_notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ defmodule BoomNotifier do
alias BoomNotifier.ErrorInfo
alias BoomNotifier.NotificationSender

@spec notify_error(Keyword.t() | Atom, Plug.Conn.t(), error :: any()) :: nil
@spec to_config(Keyword.t() | Atom) :: Keyword.t()
def to_config(config) when is_atom(config),
do: config.boom_config()

def to_config(settings),
do: settings

@doc """
Runs BoomNotifier triggering logic according to the provided configuration which
can be specified either as a keyword list or as a module atom which uses BoomNotifier.
"""
def notify_error(settings, conn, error) when is_atom(settings) do
notify_error(settings.boom_config(), conn, error)
end

@spec notify_error(Keyword.t(), Plug.Conn.t(), error :: any()) :: nil
def notify_error(settings, conn, %{kind: :error, reason: %mod{}} = error) do
ignored_exceptions = Keyword.get(settings, :ignore_exceptions, [])
ignored_exceptions = Keyword.get(to_config(settings), :ignore_exceptions, [])

unless Enum.member?(ignored_exceptions, mod) do
trigger_notify_error(settings, conn, error)
Expand Down Expand Up @@ -53,24 +56,15 @@ defmodule BoomNotifier do
end

defp run_callback(settings, callback) do
missing_keys = Enum.reject([:notifier, :options], &Keyword.has_key?(settings, &1))

case missing_keys do
[] ->
callback.(settings[:notifier], settings[:options])

[missing_key] ->
Logger.error("(BoomNotifier) #{inspect(missing_key)} parameter is missing")

_ ->
Logger.error(
"(BoomNotifier) The following parameters are missing: #{inspect(missing_keys)}"
)
if Keyword.has_key?(settings, :notifier) do
callback.(settings[:notifier], settings[:options])
else
Logger.error("Parameter :notifier is missing in #{inspect(settings)}")
end
end

defp trigger_notify_error(settings, conn, error) do
custom_data = Keyword.get(settings, :custom_data, :nothing)
custom_data = Keyword.get(to_config(settings), :custom_data, :nothing)
error_info = ErrorInfo.build(error, conn, custom_data)

NotificationSender.async_trigger_notify(settings, error_info)
Expand Down
8 changes: 1 addition & 7 deletions lib/boom_notifier/notification_sender.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ defmodule BoomNotifier.NotificationSender do
It returns :ok if notification were triggered or {:schedule, time}
if it should be delayed and by how much.
"""
@spec trigger_notify(Keyword.t() | Atom, BoomNotifier.ErrorInfo.t()) ::
:ok | {:schedule, non_neg_integer()}

def trigger_notify(settings, error_info) when is_atom(settings) do
trigger_notify(settings.boom_config(), error_info)
end

def trigger_notify(settings, error_info) do
settings = BoomNotifier.to_config(settings)
timeout = Keyword.get(settings, :time_limit)

ErrorStorage.store_error(error_info)
Expand Down
32 changes: 32 additions & 0 deletions test/unit/boom_notifier_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule BoomNotifier.BoomNotifierTest do
use BoomNotifier.Case

@receive_timeout 100

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.11, 21)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.10, 22)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.12, 22)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.10, 21)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.12, 24)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.11, 22)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.11, 24)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.12, 24)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.10, 23)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.12, 23)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.11, 23)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.13, 24)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.14, 25)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.13, 22)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.14, 24)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.14, 23)

module attribute @receive_timeout was set but never used

Check warning on line 4 in test/unit/boom_notifier_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.13, 23)

module attribute @receive_timeout was set but never used

defmodule FakeNotifier do
def notify(_, _), do: nil
end

describe "to_config/1" do
defmodule ToConfigEndpoint do
def call(conn, _opts), do: conn

use BoomNotifier,
notifier: FakeNotifier
end

test "accepts a module name" do
assert BoomNotifier.to_config(ToConfigEndpoint) == ToConfigEndpoint.boom_config()
end

test "it calls boom_notifier when a module is specified" do
assert_raise(UndefinedFunctionError, fn ->
BoomNotifier.to_config(Kernel)
end)
end

test "accepts a keyword list" do
assert BoomNotifier.to_config(notifier: FakeNotifier) == ToConfigEndpoint.boom_config()
end
end
end
20 changes: 1 addition & 19 deletions test/unit/notifier_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -319,24 +319,6 @@ defmodule NotifierTest do
:elixir_config.put(:ignore_module_conflict, false)
end

test "logs when parameters in config are missing" do
:elixir_config.put(:ignore_module_conflict, true)

conn = conn(:get, "/")

assert capture_log(fn ->
defmodule PlugLogWithMissingParameterNotifier do
use BoomNotifier, other: nil

def call(_conn, _opts) do
raise TestException.exception([])
end
end
end) =~ "(BoomNotifier) The following parameters are missing: [:notifier, :options]"

:elixir_config.put(:ignore_module_conflict, false)
end

test "logs when one parameter in config is missing" do
conn = conn(:get, "/")

Expand All @@ -351,7 +333,7 @@ defmodule NotifierTest do
raise TestException.exception([])
end
end
end) =~ "(BoomNotifier) :notifier parameter is missing"
end) =~ "Parameter :notifier is missing"
end

describe "ignored exceptions" do
Expand Down

0 comments on commit 1be6c5d

Please sign in to comment.