|
|
@ -13,6 +13,8 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
require Logger |
|
|
|
|
|
|
|
|
|
|
|
import Plug.Conn |
|
|
|
import Plug.Conn |
|
|
|
import Phoenix.Controller, only: [put_view: 2] |
|
|
|
import Phoenix.Controller, only: [put_view: 2] |
|
|
|
|
|
|
|
|
|
|
@ -28,12 +30,28 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do |
|
|
|
{:ok, conn} <- call_controller(conn, controller, action) do |
|
|
|
{:ok, conn} <- call_controller(conn, controller, action) do |
|
|
|
conn |
|
|
|
conn |
|
|
|
else |
|
|
|
else |
|
|
|
_ -> |
|
|
|
{:error, :no_action} -> |
|
|
|
conn |
|
|
|
conn |
|
|
|
|> put_status(400) |
|
|
|
|> put_status(400) |
|
|
|
|> put_view(RPCView) |
|
|
|
|> put_view(RPCView) |
|
|
|
|> Controller.render(:error, error: "Unknown action") |
|
|
|
|> Controller.render(:error, error: "Unknown action") |
|
|
|
|> halt() |
|
|
|
|> halt() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{:error, error} -> |
|
|
|
|
|
|
|
Logger.error(fn -> ["Error while calling RPC action", inspect(error)] end) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
conn |
|
|
|
|
|
|
|
|> put_status(500) |
|
|
|
|
|
|
|
|> put_view(RPCView) |
|
|
|
|
|
|
|
|> Controller.render(:error, error: "Something went wrong.") |
|
|
|
|
|
|
|
|> halt() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_ -> |
|
|
|
|
|
|
|
conn |
|
|
|
|
|
|
|
|> put_status(500) |
|
|
|
|
|
|
|
|> put_view(RPCView) |
|
|
|
|
|
|
|
|> Controller.render(:error, error: "Something went wrong.") |
|
|
|
|
|
|
|
|> halt() |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
@ -46,27 +64,35 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@doc false |
|
|
|
@doc false |
|
|
|
@spec translate_module(map(), String.t()) :: {:ok, module()} | :error |
|
|
|
@spec translate_module(map(), String.t()) :: {:ok, module()} | {:error, :no_action} |
|
|
|
def translate_module(translations, module) do |
|
|
|
defp translate_module(translations, module) do |
|
|
|
module_lowercase = String.downcase(module) |
|
|
|
module_lowercase = String.downcase(module) |
|
|
|
Map.fetch(translations, module_lowercase) |
|
|
|
|
|
|
|
|
|
|
|
case Map.fetch(translations, module_lowercase) do |
|
|
|
|
|
|
|
{:ok, module} -> {:ok, module} |
|
|
|
|
|
|
|
_ -> {:error, :no_action} |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@doc false |
|
|
|
@doc false |
|
|
|
@spec translate_action(String.t()) :: {:ok, atom()} | :error |
|
|
|
@spec translate_action(String.t()) :: {:ok, atom()} | {:error, :no_action} |
|
|
|
def translate_action(action) do |
|
|
|
defp translate_action(action) do |
|
|
|
action_lowercase = String.downcase(action) |
|
|
|
action_lowercase = String.downcase(action) |
|
|
|
{:ok, String.to_existing_atom(action_lowercase)} |
|
|
|
{:ok, String.to_existing_atom(action_lowercase)} |
|
|
|
rescue |
|
|
|
rescue |
|
|
|
ArgumentError -> :error |
|
|
|
ArgumentError -> {:error, :no_action} |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@doc false |
|
|
|
@doc false |
|
|
|
@spec call_controller(Conn.t(), module(), atom()) :: {:ok, Conn.t()} | :error |
|
|
|
@spec call_controller(Conn.t(), module(), atom()) :: {:ok, Conn.t()} | {:error, :no_action} | {:error, Exception.t()} |
|
|
|
def call_controller(conn, controller, action) do |
|
|
|
defp call_controller(conn, controller, action) do |
|
|
|
{:ok, controller.call(conn, action)} |
|
|
|
if :erlang.function_exported(controller, action, 2) do |
|
|
|
|
|
|
|
{:ok, controller.call(conn, action)} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{:error, :no_action} |
|
|
|
|
|
|
|
end |
|
|
|
rescue |
|
|
|
rescue |
|
|
|
Conn.WrapperError -> |
|
|
|
e -> |
|
|
|
:error |
|
|
|
{:error, Exception.format(:error, e, __STACKTRACE__)} |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|