Skip to content

Commit

Permalink
Merge pull request #8 from Logflare/feat/ex-on-error-callback
Browse files Browse the repository at this point in the history
feat: add :on_error callback
  • Loading branch information
Ziinc authored Oct 12, 2023
2 parents 8c07c49 + d237880 commit 5d6bc7c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
14 changes: 12 additions & 2 deletions logflare-ex/lib/logflare_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,18 @@ defmodule LogflareEx do
body = Bertex.encode(%{"batch" => batch, "source" => client.source_token})

case Tesla.post(client.tesla_client, "/api/logs", body) do
%Tesla.Env{status: 201, body: body} -> {:ok, Jason.decode!(body)}
%Tesla.Env{} = result -> {:error, result}
%Tesla.Env{status: 201, body: body} ->
{:ok, Jason.decode!(body)}

%Tesla.Env{} = result ->
# on_error callback
case Map.get(client, :on_error) do
{m, f, 1} -> apply(m, f, [result])
cb when is_function(cb) -> cb.(result)
_ -> :noop
end

{:error, result}
end
end
end
6 changes: 4 additions & 2 deletions logflare-ex/lib/logflare_ex/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule LogflareEx.Client do
field(:api_key, String.t(), enforce: true)
field(:api_url, String.t(), default: "https://api.logflare.app")
field(:source_token, String.t())
field(:on_error, list() | mfa(), default: nil)
end

@typep opts :: [api_key: String.t(), api_url: String.t(), tesla_client: Tesla.Client.t()]
Expand All @@ -21,7 +22,8 @@ defmodule LogflareEx.Client do
api_key: get_config_value(:api_key),
adapter: get_config_value(:adapter) || @default_tesla_adapter,
source_token: get_config_value(:source_token),
tesla_client: nil
tesla_client: nil,
on_error: get_config_value(:on_error)
})

tesla_client =
Expand Down Expand Up @@ -53,7 +55,7 @@ defmodule LogflareEx.Client do
Tesla.client(middlewares, adapter)
end

defp get_config_value(key) do
def get_config_value(key) do
Application.get_env(:logflare_ex, key)
end
end
9 changes: 7 additions & 2 deletions logflare-ex/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule LogflareEx.MixProject do
def project do
[
app: :logflare_ex,
version: "0.1.0#{@version_suffix}",
version: "0.1.1#{@version_suffix}",
build_path: "./_build",
config_path: "./config/config.exs",
deps_path: "./deps",
Expand All @@ -19,10 +19,15 @@ defmodule LogflareEx.MixProject do
"test.format": :test,
"test.compile": :test
],
package: package()
package: package(),
elixirc_paths: elixirc_paths(Mix.env())
]
end

# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]

# Run "mix help compile.app" to learn about applications.
def application do
[
Expand Down
20 changes: 20 additions & 0 deletions logflare-ex/test/logflare_ex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ defmodule LogflareExTest do
LogflareEx.client(api_key: "123") |> LogflareEx.send_events([%{some: "event"}])
end

describe "on_error" do
test "triggers on_error mfa if non-201 status is encountered" do
Tesla
|> expect(:post, 2, fn _client, _path, _body ->
%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
end

@tag :benchmark
# Bertex is way faster
test "benchmark Jason vs Bertex" do
Expand Down
5 changes: 5 additions & 0 deletions logflare-ex/test/support/test_utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule LogflareEx.TestUtils do
@moduledoc false

def stub_function(_env), do: nil
end
1 change: 1 addition & 0 deletions logflare-ex/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Mimic.copy(Tesla)
Mimic.copy(LogflareEx.TestUtils)
ExUnit.start(exclude: [:benchmark])

0 comments on commit 5d6bc7c

Please sign in to comment.