Skip to content

Commit 4773655

Browse files
committed
Validate config during initialization
1 parent 740770b commit 4773655

3 files changed

Lines changed: 47 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ In your `Application.start/2` callback, add the `LoggerTelegramBackend`:
2626
```elixir
2727
@impl true
2828
def start(_type, _args) do
29-
LoggerTelegramBackend.attach()
29+
{:ok, _pid} = LoggerTelegramBackend.attach()
3030

3131
# ...
3232
end

lib/telegram_logger_backend.ex

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule LoggerTelegramBackend do
88
99
@impl true
1010
def start(_type, _args) do
11-
LoggerTelegramBackend.attach()
11+
{:ok, _} = LoggerTelegramBackend.attach()
1212
1313
# ...
1414
end
@@ -109,7 +109,7 @@ defmodule LoggerTelegramBackend do
109109
## Example
110110
111111
iex> LoggerTelegramBackend.attach()
112-
:ok
112+
{:ok, _pid}
113113
114114
"""
115115
@doc since: "3.0.0"
@@ -172,15 +172,21 @@ defmodule LoggerTelegramBackend do
172172
@impl :gen_event
173173
def init(__MODULE__) do
174174
config = Application.get_env(:logger, __MODULE__, [])
175-
{:ok, initialize(config)}
175+
validate_config(config)
176176
end
177177

178178
@impl :gen_event
179-
def handle_call({:configure, config}, _state) do
179+
def handle_call({:configure, config}, state) do
180180
config = Keyword.merge(Application.get_env(:logger, __MODULE__), config)
181-
:ok = Application.put_env(:logger, __MODULE__, config)
182-
state = initialize(config)
183-
{:ok, :ok, state}
181+
182+
case validate_config(config) do
183+
{:ok, new_state} ->
184+
:ok = Application.put_env(:logger, __MODULE__, config)
185+
{:ok, :ok, new_state}
186+
187+
{:error, _reason} = error ->
188+
{:ok, error, state}
189+
end
184190
end
185191

186192
@impl :gen_event
@@ -216,6 +222,14 @@ defmodule LoggerTelegramBackend do
216222
Keyword.has_key?(metadata, key) and metadata_matches?(metadata, rest)
217223
end
218224

225+
defp validate_config(config) do
226+
cond do
227+
is_nil(config[:token]) -> {:error, {:missing_config, :token}}
228+
is_nil(config[:chat_id]) -> {:error, {:missing_config, :chat_id}}
229+
true -> {:ok, initialize(config)}
230+
end
231+
end
232+
219233
defp initialize(config) do
220234
%{
221235
level: config[:level],
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule LoggerTelegramBackend.InitTest do
2+
use ExUnit.Case, async: false
3+
4+
setup do
5+
on_exit(fn -> Application.delete_env(:logger, LoggerTelegramBackend) end)
6+
end
7+
8+
test "attach/1 returns error when :token is missing" do
9+
Application.put_env(:logger, LoggerTelegramBackend, chat_id: "$chat_id")
10+
11+
assert {:error, {{:missing_config, :token}, _}} = LoggerTelegramBackend.attach()
12+
end
13+
14+
test "attach/1 returns error when :chat_id is missing" do
15+
Application.put_env(:logger, LoggerTelegramBackend, token: "$token")
16+
17+
assert {:error, {{:missing_config, :chat_id}, _}} = LoggerTelegramBackend.attach()
18+
end
19+
20+
test "attach/1 returns error when config is empty" do
21+
Application.put_env(:logger, LoggerTelegramBackend, [])
22+
23+
assert {:error, {{:missing_config, :token}, _}} = LoggerTelegramBackend.attach()
24+
end
25+
end

0 commit comments

Comments
 (0)