From c1e582e85cac4557156cb8cc99b8c9e7c30681f3 Mon Sep 17 00:00:00 2001 From: William Sanches Date: Mon, 17 Dec 2018 17:19:32 -0200 Subject: [PATCH] Add better error messages --- .../features/pages/contract_verify_page.ex | 5 +---- apps/explorer/lib/explorer/chain/smart_contract.ex | 9 +++++++-- apps/explorer/lib/explorer/smart_contract/publisher.ex | 9 +++++---- .../explorer/smart_contract/solidity/code_compiler.ex | 2 +- .../smart_contract/solidity/code_compiler_test.exs | 4 ++-- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/apps/block_scout_web/test/block_scout_web/features/pages/contract_verify_page.ex b/apps/block_scout_web/test/block_scout_web/features/pages/contract_verify_page.ex index 38af90be22..995b95d481 100644 --- a/apps/block_scout_web/test/block_scout_web/features/pages/contract_verify_page.ex +++ b/apps/block_scout_web/test/block_scout_web/features/pages/contract_verify_page.ex @@ -39,10 +39,7 @@ defmodule BlockScoutWeb.ContractVerifyPage do end def validation_error do - css( - "[data-test='contract-source-code-error']", - text: "there was an error validating your contract, please try again." - ) + css("[data-test='contract-source-code-error']") end def verify_and_publish(session) do diff --git a/apps/explorer/lib/explorer/chain/smart_contract.ex b/apps/explorer/lib/explorer/chain/smart_contract.ex index dcc089dc27..07d51b2216 100644 --- a/apps/explorer/lib/explorer/chain/smart_contract.ex +++ b/apps/explorer/lib/explorer/chain/smart_contract.ex @@ -45,10 +45,15 @@ defmodule Explorer.Chain.SmartContract do |> unique_constraint(:address_hash) end - def invalid_contract_changeset(%__MODULE__{} = smart_contract, attrs) do + def invalid_contract_changeset(%__MODULE__{} = smart_contract, attrs, error) do smart_contract |> cast(attrs, [:name, :compiler_version, :optimization, :contract_source_code, :address_hash]) |> validate_required([:name, :compiler_version, :optimization, :address_hash]) - |> add_error(:contract_source_code, "there was an error validating your contract, please try again.") + |> add_error(:contract_source_code, error_message(error)) end + + defp error_message(:compilation), do: "There was an error compiling your contract." + defp error_message(:generated_bytecode), do: "Bytecode does not match, please try again." + defp error_message(:name), do: "Wrong contract name, please try again." + defp error_message(_), do: "There was an error validating your contract, please try again." end diff --git a/apps/explorer/lib/explorer/smart_contract/publisher.ex b/apps/explorer/lib/explorer/smart_contract/publisher.ex index f66d567c17..8a9b31e2b9 100644 --- a/apps/explorer/lib/explorer/smart_contract/publisher.ex +++ b/apps/explorer/lib/explorer/smart_contract/publisher.ex @@ -28,8 +28,8 @@ defmodule Explorer.SmartContract.Publisher do {:ok, %{abi: abi}} -> publish_smart_contract(address_hash, params, abi) - {:error, _} -> - {:error, unverified_smart_contract(address_hash, params)} + {:error, error} -> + {:error, unverified_smart_contract(address_hash, params, error)} end end @@ -39,13 +39,14 @@ defmodule Explorer.SmartContract.Publisher do |> Chain.create_smart_contract() end - defp unverified_smart_contract(address_hash, params) do + defp unverified_smart_contract(address_hash, params, error) do attrs = attributes(address_hash, params) changeset = SmartContract.invalid_contract_changeset( %SmartContract{address_hash: address_hash}, - attrs + attrs, + error ) %{changeset | action: :insert} diff --git a/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex b/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex index b21cd81864..115c3a1033 100644 --- a/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex +++ b/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex @@ -81,7 +81,7 @@ defmodule Explorer.SmartContract.Solidity.CodeCompiler do end end - def get_contract_info(contracts, _) when contracts == %{}, do: %{"errors" => []} + def get_contract_info(contracts, _) when contracts == %{}, do: {:error, :compilation} def get_contract_info(contracts, name) do new_versions_name = ":" <> name diff --git a/apps/explorer/test/explorer/smart_contract/solidity/code_compiler_test.exs b/apps/explorer/test/explorer/smart_contract/solidity/code_compiler_test.exs index 98a83bdd6d..a6235967f0 100644 --- a/apps/explorer/test/explorer/smart_contract/solidity/code_compiler_test.exs +++ b/apps/explorer/test/explorer/smart_contract/solidity/code_compiler_test.exs @@ -108,12 +108,12 @@ defmodule Explorer.SmartContract.Solidity.CodeCompilerTest do assert {:error, :name} == response end - test "returns an empty list of errors for empty info" do + test "returns compilation error for empty info" do name = "Name" response = CodeCompiler.get_contract_info(%{}, name) - assert %{"errors" => []} == response + assert {:error, :compilation} == response end test "the contract info is returned when the name matches" do