From 5de71c66f6ba10f10d7a190adf772f743f4adcea Mon Sep 17 00:00:00 2001 From: TzeYiing Date: Wed, 5 Jun 2024 19:06:54 +0800 Subject: [PATCH 1/2] fix: :on_error callback should handle 500s errors --- logflare-ex/lib/logflare_ex.ex | 2 +- logflare-ex/test/logflare_ex_test.exs | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/logflare-ex/lib/logflare_ex.ex b/logflare-ex/lib/logflare_ex.ex index 11650a6..d5b4669 100644 --- a/logflare-ex/lib/logflare_ex.ex +++ b/logflare-ex/lib/logflare_ex.ex @@ -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]) diff --git a/logflare-ex/test/logflare_ex_test.exs b/logflare-ex/test/logflare_ex_test.exs index 26bcccd..13e44c7 100644 --- a/logflare-ex/test/logflare_ex_test.exs +++ b/logflare-ex/test/logflare_ex_test.exs @@ -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"}) @@ -49,6 +49,25 @@ 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"}} @@ -65,6 +84,7 @@ defmodule LogflareExTest do assert {:error, %Tesla.Env{}} = LogflareEx.send_events(client, [%{some: "event"}]) end end + end describe "batching" do From 7d849cfbb0826b400e1a29568cb947be10c7aa3a Mon Sep 17 00:00:00 2001 From: TzeYiing Date: Wed, 5 Jun 2024 19:08:09 +0800 Subject: [PATCH 2/2] chore: formatting --- logflare-ex/test/logflare_ex_test.exs | 2 -- 1 file changed, 2 deletions(-) diff --git a/logflare-ex/test/logflare_ex_test.exs b/logflare-ex/test/logflare_ex_test.exs index 13e44c7..3578034 100644 --- a/logflare-ex/test/logflare_ex_test.exs +++ b/logflare-ex/test/logflare_ex_test.exs @@ -66,7 +66,6 @@ defmodule LogflareExTest do end end - test "triggers on_error mfa on tesla client error" do Tesla |> expect(:post, 2, fn _client, _path, _body -> @@ -84,7 +83,6 @@ defmodule LogflareExTest do assert {:error, %Tesla.Env{}} = LogflareEx.send_events(client, [%{some: "event"}]) end end - end describe "batching" do