From 85963ef02f6608bcbd0c43468b64e65995e0c558 Mon Sep 17 00:00:00 2001 From: Maxim Filonov <53992153+sl1depengwyn@users.noreply.github.com> Date: Tue, 31 Jan 2023 16:25:20 +0500 Subject: [PATCH] Refactor `try rescue` statements to keep stacktrace (#6786) * Refactor try rescue statements to keep stacktrace * Update CHANGELOG.md --- .credo.exs | 2 +- CHANGELOG.md | 1 + .../lib/block_scout_web/plug/redis_cookie.ex | 2 +- .../views/abi_encoded_value_view.ex | 6 ++-- apps/explorer/lib/explorer/account/notify.ex | 2 +- .../lib/explorer/chain/cache/address_sum.ex | 3 +- .../chain/cache/address_sum_minus_burnt.ex | 3 +- .../lib/explorer/chain/cache/block.ex | 3 +- .../explorer/chain/cache/gas_price_oracle.ex | 3 +- .../lib/explorer/chain/cache/gas_usage.ex | 3 +- .../lib/explorer/chain/cache/transaction.ex | 3 +- .../lib/explorer/chain/contract_method.ex | 2 +- apps/explorer/lib/explorer/chain/log.ex | 10 +++++-- .../lib/explorer/chain/transaction.ex | 22 ++++++++++++--- .../smart_contract/solidity/verifier.ex | 4 +-- .../explorer/smart_contract/vyper/verifier.ex | 2 +- .../token/instance_metadata_retriever.ex | 20 ++++++++----- .../indexer/fetcher/internal_transaction.ex | 28 ++++++++++++------- .../indexer/fetcher/replaced_transaction.ex | 2 +- .../lib/indexer/transform/token_transfers.ex | 7 +++-- 20 files changed, 86 insertions(+), 42 deletions(-) diff --git a/.credo.exs b/.credo.exs index ba4d7feb49e1..09db2f0079d9 100644 --- a/.credo.exs +++ b/.credo.exs @@ -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`) # diff --git a/CHANGELOG.md b/CHANGELOG.md index 9117383efb90..ba968a29eb0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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
diff --git a/apps/block_scout_web/lib/block_scout_web/plug/redis_cookie.ex b/apps/block_scout_web/lib/block_scout_web/plug/redis_cookie.ex index ff8d457f090b..092e5548858c 100644 --- a/apps/block_scout_web/lib/block_scout_web/plug/redis_cookie.ex +++ b/apps/block_scout_web/lib/block_scout_web/plug/redis_cookie.ex @@ -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__) ) %{} diff --git a/apps/block_scout_web/lib/block_scout_web/views/abi_encoded_value_view.ex b/apps/block_scout_web/lib/block_scout_web/views/abi_encoded_value_view.ex index 47dd54d6d510..a4a7fc66bebb 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/abi_encoded_value_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/abi_encoded_value_view.ex @@ -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 @@ -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 @@ -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 diff --git a/apps/explorer/lib/explorer/account/notify.ex b/apps/explorer/lib/explorer/account/notify.ex index fc1057929465..d1c390d979c3 100644 --- a/apps/explorer/lib/explorer/account/notify.ex +++ b/apps/explorer/lib/explorer/account/notify.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain/cache/address_sum.ex b/apps/explorer/lib/explorer/chain/cache/address_sum.ex index 01bb57fa65e4..6731bfb66017 100644 --- a/apps/explorer/lib/explorer/chain/cache/address_sum.ex +++ b/apps/explorer/lib/explorer/chain/cache/address_sum.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain/cache/address_sum_minus_burnt.ex b/apps/explorer/lib/explorer/chain/cache/address_sum_minus_burnt.ex index a8494d969082..695417344916 100644 --- a/apps/explorer/lib/explorer/chain/cache/address_sum_minus_burnt.ex +++ b/apps/explorer/lib/explorer/chain/cache/address_sum_minus_burnt.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain/cache/block.ex b/apps/explorer/lib/explorer/chain/cache/block.ex index 0c3ac1242aec..40bf6a2182e5 100644 --- a/apps/explorer/lib/explorer/chain/cache/block.ex +++ b/apps/explorer/lib/explorer/chain/cache/block.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain/cache/gas_price_oracle.ex b/apps/explorer/lib/explorer/chain/cache/gas_price_oracle.ex index 7ff2692c4d0b..6a99970dc826 100644 --- a/apps/explorer/lib/explorer/chain/cache/gas_price_oracle.ex +++ b/apps/explorer/lib/explorer/chain/cache/gas_price_oracle.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain/cache/gas_usage.ex b/apps/explorer/lib/explorer/chain/cache/gas_usage.ex index 79885a8a824a..91e54aada1ea 100644 --- a/apps/explorer/lib/explorer/chain/cache/gas_usage.ex +++ b/apps/explorer/lib/explorer/chain/cache/gas_usage.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain/cache/transaction.ex b/apps/explorer/lib/explorer/chain/cache/transaction.ex index d9d3337e17b1..3a44d9222903 100644 --- a/apps/explorer/lib/explorer/chain/cache/transaction.ex +++ b/apps/explorer/lib/explorer/chain/cache/transaction.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain/contract_method.ex b/apps/explorer/lib/explorer/chain/contract_method.ex index ed24f395ee17..05a82406c95d 100644 --- a/apps/explorer/lib/explorer/chain/contract_method.ex +++ b/apps/explorer/lib/explorer/chain/contract_method.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain/log.ex b/apps/explorer/lib/explorer/chain/log.ex index 3d3c5bdb0ee6..cf687b916d26 100644 --- a/apps/explorer/lib/explorer/chain/log.ex +++ b/apps/explorer/lib/explorer/chain/log.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain/transaction.ex b/apps/explorer/lib/explorer/chain/transaction.ex index e4eeabaaab0d..9dfcc4188ba8 100644 --- a/apps/explorer/lib/explorer/chain/transaction.ex +++ b/apps/explorer/lib/explorer/chain/transaction.ex @@ -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 @@ -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 diff --git a/apps/explorer/lib/explorer/smart_contract/solidity/verifier.ex b/apps/explorer/lib/explorer/smart_contract/solidity/verifier.ex index a7d76144a856..da13a7662a88 100644 --- a/apps/explorer/lib/explorer/smart_contract/solidity/verifier.ex +++ b/apps/explorer/lib/explorer/smart_contract/solidity/verifier.ex @@ -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 @@ -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 diff --git a/apps/explorer/lib/explorer/smart_contract/vyper/verifier.ex b/apps/explorer/lib/explorer/smart_contract/vyper/verifier.ex index 658372a22387..8de99d7e6d97 100644 --- a/apps/explorer/lib/explorer/smart_contract/vyper/verifier.ex +++ b/apps/explorer/lib/explorer/smart_contract/vyper/verifier.ex @@ -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 diff --git a/apps/explorer/lib/explorer/token/instance_metadata_retriever.ex b/apps/explorer/lib/explorer/token/instance_metadata_retriever.ex index daf6c4310985..c90174c50f1e 100644 --- a/apps/explorer/lib/explorer/token/instance_metadata_retriever.ex +++ b/apps/explorer/lib/explorer/token/instance_metadata_retriever.ex @@ -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 ) @@ -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 ) @@ -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 ) @@ -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 ) @@ -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 ) @@ -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 ) @@ -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 ) diff --git a/apps/indexer/lib/indexer/fetcher/internal_transaction.ex b/apps/indexer/lib/indexer/fetcher/internal_transaction.ex index a1f68676b6b0..9c7dd5a463cc 100644 --- a/apps/indexer/lib/indexer/fetcher/internal_transaction.ex +++ b/apps/indexer/lib/indexer/fetcher/internal_transaction.ex @@ -101,13 +101,7 @@ 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) _ -> @@ -115,7 +109,7 @@ defmodule Indexer.Fetcher.InternalTransaction 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 @@ -123,7 +117,21 @@ defmodule Indexer.Fetcher.InternalTransaction do 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 ) @@ -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 diff --git a/apps/indexer/lib/indexer/fetcher/replaced_transaction.ex b/apps/indexer/lib/indexer/fetcher/replaced_transaction.ex index 0e0aa33c9717..af64d8ba1258 100644 --- a/apps/indexer/lib/indexer/fetcher/replaced_transaction.ex +++ b/apps/indexer/lib/indexer/fetcher/replaced_transaction.ex @@ -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) diff --git a/apps/indexer/lib/indexer/transform/token_transfers.ex b/apps/indexer/lib/indexer/transform/token_transfers.ex index 77f005e92261..911e4115e271 100644 --- a/apps/indexer/lib/indexer/transform/token_transfers.ex +++ b/apps/indexer/lib/indexer/transform/token_transfers.ex @@ -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