Add better error messages

wsa-jason-decode-error
William Sanches 6 years ago
parent 3ff530c22e
commit c1e582e85c
No known key found for this signature in database
GPG Key ID: 27250E49FB133014
  1. 5
      apps/block_scout_web/test/block_scout_web/features/pages/contract_verify_page.ex
  2. 9
      apps/explorer/lib/explorer/chain/smart_contract.ex
  3. 9
      apps/explorer/lib/explorer/smart_contract/publisher.ex
  4. 2
      apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex
  5. 4
      apps/explorer/test/explorer/smart_contract/solidity/code_compiler_test.exs

@ -39,10 +39,7 @@ defmodule BlockScoutWeb.ContractVerifyPage do
end end
def validation_error do def validation_error do
css( css("[data-test='contract-source-code-error']")
"[data-test='contract-source-code-error']",
text: "there was an error validating your contract, please try again."
)
end end
def verify_and_publish(session) do def verify_and_publish(session) do

@ -45,10 +45,15 @@ defmodule Explorer.Chain.SmartContract do
|> unique_constraint(:address_hash) |> unique_constraint(:address_hash)
end end
def invalid_contract_changeset(%__MODULE__{} = smart_contract, attrs) do def invalid_contract_changeset(%__MODULE__{} = smart_contract, attrs, error) do
smart_contract smart_contract
|> cast(attrs, [:name, :compiler_version, :optimization, :contract_source_code, :address_hash]) |> cast(attrs, [:name, :compiler_version, :optimization, :contract_source_code, :address_hash])
|> validate_required([:name, :compiler_version, :optimization, :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 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 end

@ -28,8 +28,8 @@ defmodule Explorer.SmartContract.Publisher do
{:ok, %{abi: abi}} -> {:ok, %{abi: abi}} ->
publish_smart_contract(address_hash, params, abi) publish_smart_contract(address_hash, params, abi)
{:error, _} -> {:error, error} ->
{:error, unverified_smart_contract(address_hash, params)} {:error, unverified_smart_contract(address_hash, params, error)}
end end
end end
@ -39,13 +39,14 @@ defmodule Explorer.SmartContract.Publisher do
|> Chain.create_smart_contract() |> Chain.create_smart_contract()
end end
defp unverified_smart_contract(address_hash, params) do defp unverified_smart_contract(address_hash, params, error) do
attrs = attributes(address_hash, params) attrs = attributes(address_hash, params)
changeset = changeset =
SmartContract.invalid_contract_changeset( SmartContract.invalid_contract_changeset(
%SmartContract{address_hash: address_hash}, %SmartContract{address_hash: address_hash},
attrs attrs,
error
) )
%{changeset | action: :insert} %{changeset | action: :insert}

@ -81,7 +81,7 @@ defmodule Explorer.SmartContract.Solidity.CodeCompiler do
end end
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 def get_contract_info(contracts, name) do
new_versions_name = ":" <> name new_versions_name = ":" <> name

@ -108,12 +108,12 @@ defmodule Explorer.SmartContract.Solidity.CodeCompilerTest do
assert {:error, :name} == response assert {:error, :name} == response
end end
test "returns an empty list of errors for empty info" do test "returns compilation error for empty info" do
name = "Name" name = "Name"
response = CodeCompiler.get_contract_info(%{}, name) response = CodeCompiler.get_contract_info(%{}, name)
assert %{"errors" => []} == response assert {:error, :compilation} == response
end end
test "the contract info is returned when the name matches" do test "the contract info is returned when the name matches" do

Loading…
Cancel
Save