added error handling, filetypes check

pull/4299/head
nikitosing 4 years ago
parent 480a32603b
commit e358e61adc
  1. 96
      apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/contract_controller.ex

@ -61,23 +61,93 @@ defmodule BlockScoutWeb.API.RPC.ContractController do
get_metadata_and_publish(address_hash, conn) get_metadata_and_publish(address_hash, conn)
_ -> _ ->
files_array = VerificationController.prepare_files_array(files) with {:ok, files_array} <- prepare_params(files),
{:ok, validated_files} <- validate_files(files_array) do
verify_and_publish(address_hash, validated_files, conn)
else
{:error, error} ->
render(conn, :error, error: error)
_ ->
render(conn, :error, error: "Invalid body")
end
end
end
end
json_files = defp prepare_params(files) when is_struct(files) do
files_array {:error, "Invalid args format"}
|> Enum.filter(fn file -> file.content_type == "application/json" end) end
json_file = json_files |> Enum.at(0) defp prepare_params(files) when is_map(files) do
{:ok, VerificationController.prepare_files_array(files)}
end
if json_file do defp prepare_params(files) when is_list(files) do
verify_and_publish(address_hash, files_array, conn) {:ok, files}
else end
render(conn, :error, error: "Please attach JSON file with metadata of contract's compilation.")
end defp prepare_params(_arg) do
{:error, "Invalid args format"}
end
defp validate_files(files) do
if length(files) < 2 do
{:error, "You should attach at least 2 files"}
else
files_array =
files
|> Enum.filter(fn file -> validate_filename(file.filename) end)
jsons =
files_array
|> Enum.filter(fn file -> only_json(file.filename) end)
sols =
files_array
|> Enum.filter(fn file -> only_sol(file.filename) end)
if length(jsons) > 0 and length(sols) > 0 do
{:ok, files_array}
else
{:error, "You should attach at least one *.json and one *.sol files"}
end end
end end
end end
defp validate_filename(filename) do
case List.last(String.split(String.downcase(filename), ".")) do
"sol" ->
true
"json" ->
true
_ ->
false
end
end
defp only_sol(filename) do
case List.last(String.split(String.downcase(filename), ".")) do
"sol" ->
true
_ ->
false
end
end
defp only_json(filename) do
case List.last(String.split(String.downcase(filename), ".")) do
"json" ->
true
_ ->
false
end
end
defp get_metadata_and_publish(address_hash_string, conn) do defp get_metadata_and_publish(address_hash_string, conn) do
case Sourcify.get_metadata(address_hash_string) do case Sourcify.get_metadata(address_hash_string) do
{:ok, verification_metadata} -> {:ok, verification_metadata} ->
@ -113,10 +183,16 @@ defmodule BlockScoutWeb.API.RPC.ContractController do
{:error, %{"error" => error}} -> {:error, %{"error" => error}} ->
render(conn, :error, error: error) render(conn, :error, error: error)
{:error, error} ->
render(conn, :error, error: error)
end end
{:error, %{"error" => error}} -> {:error, %{"error" => error}} ->
render(conn, :error, error: error) render(conn, :error, error: error)
{:error, error} ->
render(conn, :error, error: error)
end end
end end

Loading…
Cancel
Save