Skip to content

Commit

Permalink
Merge pull request #24 from Logflare/fix/on-error-callback-500s
Browse files Browse the repository at this point in the history
fix: :on_error callback should handle 500s errors
  • Loading branch information
Ziinc authored Jun 10, 2024
2 parents 8521594 + 7d849cf commit 9ad6f45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
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

0 comments on commit 9ad6f45

Please sign in to comment.