Elixir 参数错误:erlang.apply(#Mod.client/1中的函数<;xx>;,:t,[])

Elixir 参数错误:erlang.apply(#Mod.client/1中的函数<;xx>;,:t,[]),elixir,metaprogramming,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

我有以下资料:

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_客户端的源代码,它看起来好像返回了一个struct:

defmacro build_client(pre, post \\ []) do
  quote do
    %Tesla.Client{
      pre:  Tesla.prepare(__MODULE__, unquote(pre)),
      post: Tesla.prepare(__MODULE__, unquote(post))
    }
  end
end
其中特斯拉客户的定义如下:

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
这一定是某种元编程的魔力,但我很难理解它

编辑:我实际上要做的是在
get
调用之前检查
客户机
,以便检查url、标题等

完整堆栈跟踪:

[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
[错误]#运行ApiWeb的PID。终结点已终止
服务器:本地主机:4000(http)
请求:GET/api/test
**(退出)引发了一个异常:
**(ArgumentError)参数错误
:erlang.apply(#函数,: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:ApiWeb.plugs.subject.get_uid_from_cache/2中的匿名fn/2
(cachex)lib/cachex/util.ex:77:cachex.util.get_fallback/4
(cachex)lib/cachex/actions/get.ex:42:cachex.actions.get.handle_记录/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:ApiWeb.router
(凤凰城)lib/phoenix/router.ex:273:phoenix.router.\uuuuu呼叫\uuuuu/1
(api_web)lib/api_web/endpoint.ex:1:ApiWeb.endpoint.plug_builder_call/2
(api_web)lib/plug/debugger.ex:99:ApiWeb.Endpoint.“调用(可重写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
(牛仔)/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
def用户\u托管的\u虚拟机(客户端,uid)执行
url=“/getuser?userID=”uid
如果(配置(:api_web)[:env]!=:prod)执行
IO.inspect client.t()
warn(“对端点#{url}上的#{inspect(client.t)}}的实际请求”)
结束
获取(客户端,url)
结束

只有在“打印”客户机时才会出现错误?你怎么印的?我认为任何打印函数都不会抛出这种错误。对不起,我是在事后添加的。IO.inspect。请发布完整的错误消息,包括堆栈跟踪。我在删除
.t()
堆栈跟踪时将其添加到问题中,但现在IO.inspect打印
#函数
。这就是这个搜索的开始。只有在“打印”客户机时才会出现错误?你怎么印的?我认为任何打印函数都不会抛出这种错误。对不起,我是在事后添加的。IO.inspect。请发布完整的错误消息,包括堆栈跟踪。我在删除
.t()
堆栈跟踪时将其添加到问题中,但现在IO.inspect打印
#函数
。这就是这次狩猎的开始。