参数错误,:erlang.apply(#Function <xx in =“”mod.client =“”1 =“”>,:t,[])

时间:2017-10-13 11:27:15

标签: elixir metaprogramming

我有以下内容:

defmodule ApiWeb.Helpers.Oapi do
  import Plug.Conn
  use Tesla, only: ~w(get)a
  require Logger

  plug Tesla.Middleware.BaseUrl, "example.com"

  def client(conn) do
    Tesla.build_client [
      {Tesla.Middleware.Headers, %{"Authorization" => get_req_header(conn, "authorization") }}
    ]
  end
在其他地方,我打电话给:

client = Oapi.client(conn)

当我打印client时,我收到上述错误,暗示它是一个函数。但是,如果我查看Tesla.build_client的源代码,它看起来好像是返回一个结构:

defmacro build_client(pre, post \\ []) do
  quote do
    %Tesla.Client{
      pre:  Tesla.prepare(__MODULE__, unquote(pre)),
      post: Tesla.prepare(__MODULE__, unquote(post))
    }
  end
end

Tesla.Client定义如下:

defmodule Tesla.Client do
  @type t :: %__MODULE__{
            fun:  (Tesla.Env.t, Tesla.Env.stack -> Tesla.Env.t),
            pre:  Tesla.Env.stack,
            post: Tesla.Env.stack
  }
  defstruct fun: nil,
            pre: [],
            post: []
end

这必须是一些元编程魔术,但我无法理解它。

编辑:我实际上要做的是在client来电之前检查get,这样我就可以检查网址,标题等。

完整的堆栈跟踪:

[error] #PID<0.522.0> running ApiWeb.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /api/test
** (exit) an exception was raised:
    ** (ArgumentError) argument error
        :erlang.apply(#Function<0.84960178/2 in ApiWeb.Helpers.Oapi.client/1>, :t, [])
        (api_web) lib/helpers/oapi.ex:27: ApiWeb.Helpers.Oapi.users_hosted_vms/2
        (api_web) lib/plugs/subject.ex:80: ApiWeb.Plugs.Subject.load_from_oapi/2
        (api_web) lib/plugs/subject.ex:40: anonymous fn/2 in ApiWeb.Plugs.Subject.get_uid_from_cache/2
        (cachex) lib/cachex/util.ex:77: Cachex.Util.get_fallback/4
        (cachex) lib/cachex/actions/get.ex:42: Cachex.Actions.Get.handle_record/4
        (cachex) lib/cachex/actions/get.ex:28: Cachex.Actions.Get.execute/3
        (api_web) lib/plugs/subject.ex:37: ApiWeb.Plugs.Subject.get_uid_from_cache/2
        (api_web) lib/api_web/router.ex:16: ApiWeb.Router.api/2
        (api_web) lib/api_web/router.ex:1: anonymous fn/1 in ApiWeb.Router.__match_route__/4
        (phoenix) lib/phoenix/router.ex:273: Phoenix.Router.__call__/1
        (api_web) lib/api_web/endpoint.ex:1: ApiWeb.Endpoint.plug_builder_call/2
        (api_web) lib/plug/debugger.ex:99: ApiWeb.Endpoint."call (overridable 3)"/2
        (api_web) lib/api_web/endpoint.ex:1: ApiWeb.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /Users/raarts/work/company/api-elixir/api/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

这是产生错误的函数:

def users_hosted_vms(client, uid) do
  url = "/getuser?userID=" <> uid
  if (config(:api_web)[:env] != :prod) do 
    IO.inspect client.t()
    Logger.warn("ACTUAL REQUEST TO #{inspect(client.t)}} on endpoint #{url}")
  end 
  get(client, url)
end

0 个答案:

没有答案