Skip to content

Commit c4721af

Browse files
authored
add request error logs (#176)
* add request error logs * typo
1 parent 3dbbf6c commit c4721af

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

config/config.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ config :ethereumex,
44
http_options: [pool_timeout: 5000, receive_timeout: 15_000],
55
http_pool_options: %{},
66
http_headers: [],
7-
json_module: Jason
7+
json_module: Jason,
8+
enable_request_error_logs: false
89

910
# config :ethereumex, ipc_path: "/Library/Application Support/io.parity.ethereum/jsonrpc.ipc"
1011
import_config "#{Mix.env()}.exs"

lib/ethereumex/config.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ defmodule Ethereumex.Config do
8484
Application.get_env(:ethereumex, :client_type, :http)
8585
end
8686

87+
@spec enable_request_error_logs() :: boolean()
88+
def enable_request_error_logs() do
89+
Application.get_env(:ethereumex, :enable_request_error_logs, false)
90+
end
91+
8792
@spec format_batch() :: boolean()
8893
def format_batch() do
8994
Application.get_env(:ethereumex, :format_batch, true)

lib/ethereumex/http_client.ex

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ defmodule Ethereumex.HttpClient do
55

66
alias Ethereumex.Config
77

8+
require Logger
9+
810
@type empty_response :: :empty_response
911
@type invalid_json :: {:invalid_json, any()}
1012
@type http_client_error :: {:error, empty_response() | invalid_json() | any()}
@@ -24,9 +26,23 @@ defmodule Ethereumex.HttpClient do
2426

2527
case Finch.request(request, Ethereumex.Finch, Config.http_options()) do
2628
{:ok, %Finch.Response{body: body, status: code}} ->
27-
decode_body(body, code, format_batch)
29+
case decode_body(body, code, format_batch) do
30+
{:ok, _response} = result ->
31+
result
32+
33+
error ->
34+
maybe_log_error(
35+
"[#{__MODULE__}] Decode failed, body - #{inspect(body)}, payload - #{inspect(payload)}, error - #{inspect(error)}"
36+
)
37+
38+
error
39+
end
2840

2941
{:error, error} ->
42+
maybe_log_error(
43+
"[#{__MODULE__}] Request failed, payload - #{inspect(payload)}, error - #{inspect(error)}"
44+
)
45+
3046
{:error, error}
3147
end
3248
end
@@ -63,4 +79,12 @@ defmodule Ethereumex.HttpClient do
6379
defp maybe_format_batch(responses, true), do: format_batch(responses)
6480

6581
defp maybe_format_batch(responses, _), do: responses
82+
83+
defp maybe_log_error(message) do
84+
if Config.enable_request_error_logs() do
85+
Logger.error(message)
86+
else
87+
:ok
88+
end
89+
end
6690
end

0 commit comments

Comments
 (0)