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

fix: :on_error callback should handle 500s errors #24

Merged
merged 2 commits into from
Jun 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
2 changes: 1 addition & 1 deletion logflare-ex/lib/logflare_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule LogflareEx do
{:ok, %Tesla.Env{status: status, body: body}} when status < 300 ->
{:ok, Jason.decode!(body)}

{:error, %Tesla.Env{} = result} ->
{_result, %Tesla.Env{} = result} ->
# on_error callback
case Map.get(client, :on_error) do
{m, f, 1} -> apply(m, f, [result])
Expand Down
20 changes: 19 additions & 1 deletion logflare-ex/test/logflare_ex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule LogflareExTest do

Tesla
|> expect(:post, fn _client, _path, _body ->
{:error, %Tesla.Env{status: 500, body: "server err"}}
{:ok, %Tesla.Env{status: 500, body: "server err"}}
end)

assert {:error, %Tesla.Env{}} = LogflareEx.send_event(client, %{some: "event"})
Expand All @@ -49,6 +49,24 @@ defmodule LogflareExTest do

describe "on_error" do
test "triggers on_error mfa if non-201 status is encountered" do
Tesla
|> expect(:post, 2, fn _client, _path, _body ->
{:ok, %Tesla.Env{status: 500, body: "some server error"}}
end)

LogflareEx.TestUtils
|> expect(:stub_function, 2, fn %{status: 500} -> :ok end)

for cb <- [
{LogflareEx.TestUtils, :stub_function, 1},
&LogflareEx.TestUtils.stub_function/1
] do
client = LogflareEx.client(api_key: "123", source_token: "123", on_error: cb)
assert {:error, %Tesla.Env{}} = LogflareEx.send_events(client, [%{some: "event"}])
end
end

test "triggers on_error mfa on tesla client error" do
Tesla
|> expect(:post, 2, fn _client, _path, _body ->
{:error, %Tesla.Env{status: 500, body: "some server error"}}
Expand Down
Loading