Skip to content

Commit 1be6c5d

Browse files
committed
pass around a boom module instead of settings
1 parent 0c8c8bf commit 1be6c5d

File tree

4 files changed

+48
-46
lines changed

4 files changed

+48
-46
lines changed

lib/boom_notifier.ex

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ defmodule BoomNotifier do
88
alias BoomNotifier.ErrorInfo
99
alias BoomNotifier.NotificationSender
1010

11-
@spec notify_error(Keyword.t() | Atom, Plug.Conn.t(), error :: any()) :: nil
11+
@spec to_config(Keyword.t() | Atom) :: Keyword.t()
12+
def to_config(config) when is_atom(config),
13+
do: config.boom_config()
14+
15+
def to_config(settings),
16+
do: settings
17+
1218
@doc """
1319
Runs BoomNotifier triggering logic according to the provided configuration which
1420
can be specified either as a keyword list or as a module atom which uses BoomNotifier.
1521
"""
16-
def notify_error(settings, conn, error) when is_atom(settings) do
17-
notify_error(settings.boom_config(), conn, error)
18-
end
19-
22+
@spec notify_error(Keyword.t(), Plug.Conn.t(), error :: any()) :: nil
2023
def notify_error(settings, conn, %{kind: :error, reason: %mod{}} = error) do
21-
ignored_exceptions = Keyword.get(settings, :ignore_exceptions, [])
24+
ignored_exceptions = Keyword.get(to_config(settings), :ignore_exceptions, [])
2225

2326
unless Enum.member?(ignored_exceptions, mod) do
2427
trigger_notify_error(settings, conn, error)
@@ -53,24 +56,15 @@ defmodule BoomNotifier do
5356
end
5457

5558
defp run_callback(settings, callback) do
56-
missing_keys = Enum.reject([:notifier, :options], &Keyword.has_key?(settings, &1))
57-
58-
case missing_keys do
59-
[] ->
60-
callback.(settings[:notifier], settings[:options])
61-
62-
[missing_key] ->
63-
Logger.error("(BoomNotifier) #{inspect(missing_key)} parameter is missing")
64-
65-
_ ->
66-
Logger.error(
67-
"(BoomNotifier) The following parameters are missing: #{inspect(missing_keys)}"
68-
)
59+
if Keyword.has_key?(settings, :notifier) do
60+
callback.(settings[:notifier], settings[:options])
61+
else
62+
Logger.error("Parameter :notifier is missing in #{inspect(settings)}")
6963
end
7064
end
7165

7266
defp trigger_notify_error(settings, conn, error) do
73-
custom_data = Keyword.get(settings, :custom_data, :nothing)
67+
custom_data = Keyword.get(to_config(settings), :custom_data, :nothing)
7468
error_info = ErrorInfo.build(error, conn, custom_data)
7569

7670
NotificationSender.async_trigger_notify(settings, error_info)

lib/boom_notifier/notification_sender.ex

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,8 @@ defmodule BoomNotifier.NotificationSender do
2525
It returns :ok if notification were triggered or {:schedule, time}
2626
if it should be delayed and by how much.
2727
"""
28-
@spec trigger_notify(Keyword.t() | Atom, BoomNotifier.ErrorInfo.t()) ::
29-
:ok | {:schedule, non_neg_integer()}
30-
31-
def trigger_notify(settings, error_info) when is_atom(settings) do
32-
trigger_notify(settings.boom_config(), error_info)
33-
end
34-
3528
def trigger_notify(settings, error_info) do
29+
settings = BoomNotifier.to_config(settings)
3630
timeout = Keyword.get(settings, :time_limit)
3731

3832
ErrorStorage.store_error(error_info)

test/unit/boom_notifier_test.exs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule BoomNotifier.BoomNotifierTest do
2+
use BoomNotifier.Case
3+
4+
@receive_timeout 100
5+
6+
defmodule FakeNotifier do
7+
def notify(_, _), do: nil
8+
end
9+
10+
describe "to_config/1" do
11+
defmodule ToConfigEndpoint do
12+
def call(conn, _opts), do: conn
13+
14+
use BoomNotifier,
15+
notifier: FakeNotifier
16+
end
17+
18+
test "accepts a module name" do
19+
assert BoomNotifier.to_config(ToConfigEndpoint) == ToConfigEndpoint.boom_config()
20+
end
21+
22+
test "it calls boom_notifier when a module is specified" do
23+
assert_raise(UndefinedFunctionError, fn ->
24+
BoomNotifier.to_config(Kernel)
25+
end)
26+
end
27+
28+
test "accepts a keyword list" do
29+
assert BoomNotifier.to_config(notifier: FakeNotifier) == ToConfigEndpoint.boom_config()
30+
end
31+
end
32+
end

test/unit/notifier_test.exs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -319,24 +319,6 @@ defmodule NotifierTest do
319319
:elixir_config.put(:ignore_module_conflict, false)
320320
end
321321

322-
test "logs when parameters in config are missing" do
323-
:elixir_config.put(:ignore_module_conflict, true)
324-
325-
conn = conn(:get, "/")
326-
327-
assert capture_log(fn ->
328-
defmodule PlugLogWithMissingParameterNotifier do
329-
use BoomNotifier, other: nil
330-
331-
def call(_conn, _opts) do
332-
raise TestException.exception([])
333-
end
334-
end
335-
end) =~ "(BoomNotifier) The following parameters are missing: [:notifier, :options]"
336-
337-
:elixir_config.put(:ignore_module_conflict, false)
338-
end
339-
340322
test "logs when one parameter in config is missing" do
341323
conn = conn(:get, "/")
342324

@@ -351,7 +333,7 @@ defmodule NotifierTest do
351333
raise TestException.exception([])
352334
end
353335
end
354-
end) =~ "(BoomNotifier) :notifier parameter is missing"
336+
end) =~ "Parameter :notifier is missing"
355337
end
356338

357339
describe "ignored exceptions" do

0 commit comments

Comments
 (0)