Skip to content

Commit

Permalink
Refactor try rescue statements to keep stacktrace (blockscout#6786)
Browse files Browse the repository at this point in the history
* Refactor try rescue statements to keep stacktrace

* Update CHANGELOG.md
  • Loading branch information
sl1depengwyn authored Jan 31, 2023
1 parent cfc1fa4 commit 85963ef
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
{Credo.Check.Warning.UnusedRegexOperation},
{Credo.Check.Warning.UnusedStringOperation},
{Credo.Check.Warning.UnusedTupleOperation},
{Credo.Check.Warning.RaiseInsideRescue, false},
{Credo.Check.Warning.RaiseInsideRescue},

# Controversial and experimental checks (opt-in, just remove `, false`)
#
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

### Chore

- [#6786](https://github.com/blockscout/blockscout/pull/6786) - Refactor `try rescue` statements to keep stacktrace
- [#6695](https://github.com/blockscout/blockscout/pull/6695) - Process errors and warnings with enables check-js feature in VS code

<details>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ defmodule BlockScoutWeb.Plug.RedisCookie do
Logger.log(
log,
"Plug.Session could not decode incoming session cookie. Reason: " <>
Exception.message(e)
Exception.format(:error, e, __STACKTRACE__)
)

%{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule BlockScoutWeb.ABIEncodedValueView do
rescue
exception ->
Logger.warn(fn ->
["Error determining value html for #{inspect(type)}: ", Exception.format(:error, exception)]
["Error determining value html for #{inspect(type)}: ", Exception.format(:error, exception, __STACKTRACE__)]
end)

:error
Expand All @@ -34,7 +34,7 @@ defmodule BlockScoutWeb.ABIEncodedValueView do
rescue
exception ->
Logger.warn(fn ->
["Error determining value json for #{inspect(type)}: ", Exception.format(:error, exception)]
["Error determining value json for #{inspect(type)}: ", Exception.format(:error, exception, __STACKTRACE__)]
end)

nil
Expand All @@ -47,7 +47,7 @@ defmodule BlockScoutWeb.ABIEncodedValueView do
rescue
exception ->
Logger.warn(fn ->
["Error determining copy text for #{inspect(type)}: ", Exception.format(:error, exception)]
["Error determining copy text for #{inspect(type)}: ", Exception.format(:error, exception, __STACKTRACE__)]
end)

:error
Expand Down
2 changes: 1 addition & 1 deletion apps/explorer/lib/explorer/account/notify.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Explorer.Account.Notify do
rescue
err ->
Logger.info("--- Notifier error", fetcher: :account)
Logger.info(err, fetcher: :account)
:error |> Exception.format(err, __STACKTRACE__) |> Logger.info(fetcher: :account)
end

defp check_envs do
Expand Down
3 changes: 2 additions & 1 deletion apps/explorer/lib/explorer/chain/cache/address_sum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ defmodule Explorer.Chain.Cache.AddressSum do
rescue
e ->
Logger.debug([
"Coudn't update address sum test #{inspect(e)}"
"Coudn't update address sum: ",
Exception.format(:error, e, __STACKTRACE__)
])
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ defmodule Explorer.Chain.Cache.AddressSumMinusBurnt do
rescue
e ->
Logger.debug([
"Coudn't update address sum test #{inspect(e)}"
"Coudn't update address sum: ",
Exception.format(:error, e, __STACKTRACE__)
])
end

Expand Down
3 changes: 2 additions & 1 deletion apps/explorer/lib/explorer/chain/cache/block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ defmodule Explorer.Chain.Cache.Block do
rescue
e ->
Logger.debug([
"Coudn't update block count test #{inspect(e)}"
"Coudn't update block count: ",
Exception.format(:error, e, __STACKTRACE__)
])
end

Expand Down
3 changes: 2 additions & 1 deletion apps/explorer/lib/explorer/chain/cache/gas_price_oracle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ defmodule Explorer.Chain.Cache.GasPriceOracle do
rescue
e ->
Logger.debug([
"Coudn't update gas used gas_prices #{inspect(e)}"
"Coudn't update gas used gas_prices",
Exception.format(:error, e, __STACKTRACE__)
])
end

Expand Down
3 changes: 2 additions & 1 deletion apps/explorer/lib/explorer/chain/cache/gas_usage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ defmodule Explorer.Chain.Cache.GasUsage do
rescue
e ->
Logger.debug([
"Coudn't update gas used sum test #{inspect(e)}"
"Coudn't update gas used sum: ",
Exception.format(:error, e, __STACKTRACE__)
])
end

Expand Down
3 changes: 2 additions & 1 deletion apps/explorer/lib/explorer/chain/cache/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ defmodule Explorer.Chain.Cache.Transaction do
rescue
e ->
Logger.debug([
"Coudn't update transaction count test #{inspect(e)}"
"Coudn't update transaction count: ",
Exception.format(:error, e, __STACKTRACE__)
])
end

Expand Down
2 changes: 1 addition & 1 deletion apps/explorer/lib/explorer/chain/contract_method.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule Explorer.Chain.ContractMethod do
end
rescue
e ->
message = Exception.format(:error, e)
message = Exception.format(:error, e, __STACKTRACE__)

{:error, message}
end
Expand Down
10 changes: 8 additions & 2 deletions apps/explorer/lib/explorer/chain/log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,14 @@ defmodule Explorer.Chain.Log do
{:ok, selector, mapping}
end
rescue
_ ->
Logger.warn(fn -> ["Could not decode input data for log from transaction: ", Hash.to_iodata(transaction.hash)] end)
e ->
Logger.warn(fn ->
[
"Could not decode input data for log from transaction: ",
Hash.to_iodata(transaction.hash),
Exception.format(:error, e, __STACKTRACE__)
]
end)

{:error, :could_not_decode}
end
Expand Down
22 changes: 18 additions & 4 deletions apps/explorer/lib/explorer/chain/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,15 @@ defmodule Explorer.Chain.Transaction do

{:ok, result}
rescue
_ ->
Logger.warn(fn -> ["Could not decode input data for transaction: ", Hash.to_iodata(hash)] end)
e ->
Logger.warn(fn ->
[
"Could not decode input data for transaction: ",
Hash.to_iodata(hash),
Exception.format(:error, e, __STACKTRACE__)
]
end)

{:error, :could_not_decode}
end

Expand All @@ -686,8 +693,15 @@ defmodule Explorer.Chain.Transaction do

{:ok, mapping}
rescue
_ ->
Logger.warn(fn -> ["Could not decode input data for transaction: ", Hash.to_iodata(hash)] end)
e ->
Logger.warn(fn ->
[
"Could not decode input data for transaction: ",
Hash.to_iodata(hash),
Exception.format(:error, e, __STACKTRACE__)
]
end)

{:error, :could_not_decode}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Explorer.SmartContract.Solidity.Verifier do
Logger.error(fn ->
[
"Error while verifying smart-contract address: #{address_hash}, params: #{inspect(params, limit: :infinity, printable_limit: :infinity)}: ",
Exception.format(:error, exception)
Exception.format(:error, exception, __STACKTRACE__)
]
end)
end
Expand Down Expand Up @@ -117,7 +117,7 @@ defmodule Explorer.SmartContract.Solidity.Verifier do
Logger.error(fn ->
[
"Error while verifying smart-contract address: #{address_hash}, params: #{inspect(params, limit: :infinity, printable_limit: :infinity)}, json_input: #{inspect(json_input, limit: :infinity, printable_limit: :infinity)}: ",
Exception.format(:error, exception)
Exception.format(:error, exception, __STACKTRACE__)
]
end)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Explorer.SmartContract.Vyper.Verifier do
Logger.error(fn ->
[
"Error while verifying smart-contract address: #{address_hash}, params: #{inspect(params, limit: :infinity, printable_limit: :infinity)}: ",
Exception.format(:error, exception)
Exception.format(:error, exception, __STACKTRACE__)
]
end)
end
Expand Down
20 changes: 13 additions & 7 deletions apps/explorer/lib/explorer/token/instance_metadata_retriever.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
fetch_json(%{@token_uri => {:ok, [decoded_json]}}, hex_token_id)
rescue
e ->
Logger.debug(["Unknown metadata format #{inspect(json)}. error #{inspect(e)}"],
Logger.debug(["Unknown metadata format #{inspect(json)}.", Exception.format(:error, e, __STACKTRACE__)],
fetcher: :token_instances
)

Expand All @@ -161,7 +161,7 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
fetch_json(%{@uri => {:ok, [decoded_json]}}, hex_token_id)
rescue
e ->
Logger.debug(["Unknown metadata format #{inspect(json)}. error #{inspect(e)}"],
Logger.debug(["Unknown metadata format #{inspect(json)}.", Exception.format(:error, e, __STACKTRACE__)],
fetcher: :token_instances
)

Expand All @@ -178,7 +178,11 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
end
rescue
e ->
Logger.debug(["Unknown metadata format base64 #{inspect(base64_encoded_json)}. error #{inspect(e)}"],
Logger.debug(
[
"Unknown metadata format base64 #{inspect(base64_encoded_json)}.",
Exception.format(:error, e, __STACKTRACE__)
],
fetcher: :token_instances
)

Expand All @@ -195,7 +199,8 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
end
rescue
e ->
Logger.debug(["Unknown metadata format base64 #{inspect(base64_encoded_json)}. error #{inspect(e)}"],
Logger.debug(
["Unknown metadata format base64 #{inspect(base64_encoded_json)}", Exception.format(:error, e, __STACKTRACE__)],
fetcher: :token_instances
)

Expand Down Expand Up @@ -228,7 +233,7 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
check_type(json, hex_token_id)
rescue
e ->
Logger.debug(["Unknown metadata format #{inspect(json)}. error #{inspect(e)}"],
Logger.debug(["Unknown metadata format #{inspect(json)}.", Exception.format(:error, e, __STACKTRACE__)],
fetcher: :token_instances
)

Expand All @@ -241,7 +246,7 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
check_type(json, hex_token_id)
rescue
e ->
Logger.debug(["Unknown metadata format #{inspect(json)}. error #{inspect(e)}"],
Logger.debug(["Unknown metadata format #{inspect(json)}.", Exception.format(:error, e, __STACKTRACE__)],
fetcher: :token_instances
)

Expand Down Expand Up @@ -282,7 +287,8 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
end
rescue
e ->
Logger.debug(["Could not send request to token uri #{inspect(uri)}. error #{inspect(e)}"],
Logger.debug(
["Could not send request to token uri #{inspect(uri)}.", Exception.format(:error, e, __STACKTRACE__)],
fetcher: :token_instances
)

Expand Down
28 changes: 18 additions & 10 deletions apps/indexer/lib/indexer/fetcher/internal_transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,37 @@ defmodule Indexer.Fetcher.InternalTransaction do
json_rpc_named_arguments
|> Keyword.fetch!(:variant)
|> case do
EthereumJSONRPC.Nethermind ->
EthereumJSONRPC.fetch_block_internal_transactions(filtered_unique_numbers, json_rpc_named_arguments)

EthereumJSONRPC.Erigon ->
EthereumJSONRPC.fetch_block_internal_transactions(filtered_unique_numbers, json_rpc_named_arguments)

EthereumJSONRPC.Besu ->
variant when variant in [EthereumJSONRPC.Nethermind, EthereumJSONRPC.Erigon, EthereumJSONRPC.Besu] ->
EthereumJSONRPC.fetch_block_internal_transactions(filtered_unique_numbers, json_rpc_named_arguments)

_ ->
try do
fetch_block_internal_transactions_by_transactions(filtered_unique_numbers, json_rpc_named_arguments)
rescue
error ->
{:error, error}
{:error, error, __STACKTRACE__}
end
end
|> case do
{:ok, internal_transactions_params} ->
safe_import_internal_transaction(internal_transactions_params, filtered_unique_numbers)

{:error, reason} ->
Logger.error(fn -> ["failed to fetch internal transactions for blocks: ", inspect(reason)] end,
Logger.error(
fn ->
["failed to fetch internal transactions for blocks: ", Exception.format(:error, reason)]
end,
error_count: filtered_unique_numbers_count
)

# re-queue the de-duped entries
{:retry, filtered_unique_numbers}

{:error, reason, stacktrace} ->
Logger.error(
fn ->
["failed to fetch internal transactions for blocks: ", Exception.format(:error, reason, stacktrace)]
end,
error_count: filtered_unique_numbers_count
)

Expand Down Expand Up @@ -171,7 +179,7 @@ defmodule Indexer.Fetcher.InternalTransaction do
EthereumJSONRPC.fetch_internal_transactions(transactions, json_rpc_named_arguments)
catch
:exit, error ->
{:error, error}
{:error, error, __STACKTRACE__}
end
end
|> case do
Expand Down
2 changes: 1 addition & 1 deletion apps/indexer/lib/indexer/fetcher/replaced_transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ defmodule Indexer.Fetcher.ReplacedTransaction do
Logger.error(fn ->
[
"failed to update replaced transactions for transactions: ",
inspect(reason)
Exception.format(:error, reason, __STACKTRACE__)
]
end)

Expand Down
7 changes: 5 additions & 2 deletions apps/indexer/lib/indexer/transform/token_transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ defmodule Indexer.Transform.TokenTransfers do
token_transfers: [token_transfer | token_transfers]
}
rescue
_ in [FunctionClauseError, MatchError] ->
Logger.error(fn -> "Unknown token transfer format: #{inspect(log)}" end)
e in [FunctionClauseError, MatchError] ->
Logger.error(fn ->
["Unknown token transfer format: #{inspect(log)}", Exception.format(:error, e, __STACKTRACE__)]
end)

acc
end

Expand Down

0 comments on commit 85963ef

Please sign in to comment.