简体   繁体   English

(RuntimeError)期望动作/ 2返回一个Plug.Conn,所有插件必须接收连接(conn)并返回连接

[英](RuntimeError) expected action/2 to return a Plug.Conn, all plugs must receive a connection (conn) and return a connection

In one of my controllers I've got the following code (excerpt): 在我的一个控制器中,我有以下代码(摘录):

case HTTPoison.get("https://*****.zendesk.com/api/v2/users/search.json?query=" <> clid, headers, [hackney: hackney]) do
  {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
    conn
    |> put_status(200)
    |> json(body)
  {:ok, %HTTPoison.Response{status_code: 404}} ->
    conn
    |> put_status(404)
    |> json(%{error_code: "404", reason_given: "Resource not found."})
  {:error, %HTTPoison.Error{reason: reason}} ->
    conn
    |> put_status(500)
    |> json(%{error_code: "500", reason_given: "None."})
end

When I run the code, it works fine, but Phoenix throws the runtime exception: 当我运行代码时,它工作正常,但Phoenix抛出运行时异常:

** (exit) an exception was raised:
    ** (RuntimeError) expected action/2 to return a Plug.Conn, all plugs must receive a connection (conn) and return a connection
        (zentonies) web/controllers/page_controller.ex:1: Zentonies.PageController.phoenix_controller_pipeline/2
        (zentonies) lib/zentonies/endpoint.ex:1: Zentonies.Endpoint.instrument/4
        (zentonies) lib/phoenix/router.ex:261: Zentonies.Router.dispatch/2
        (zentonies) web/router.ex:1: Zentonies.Router.do_call/2
        (zentonies) lib/zentonies/endpoint.ex:1: Zentonies.Endpoint.phoenix_pipeline/1
        (zentonies) lib/plug/debugger.ex:93: Zentonies.Endpoint."call (overridable 3)"/2
        (zentonies) lib/zentonies/endpoint.ex:1: Zentonies.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

What am I doing wrong? 我究竟做错了什么?

The stack trace is telling you that your controller action is not returning a Plug.Conn struct. 堆栈跟踪告诉您控制器操作未返回Plug.Conn结构。 In Elixir, the result of the very last expression of a function is returned. 在Elixir中,返回函数最后一个表达式的结果。 Look at the last line of your function and ensure it is returning the result of your case expression. 查看函数的最后一行并确保它返回case表达式的结果。

def(conn, params) do
  final_conn = 
    case HTTPoison.get("https://*****.zendesk.com/api/v2/users/search.json?query=" <> clid, headers, [hackney: hackney]) do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        conn
        |> put_status(200)
        |> json(body)
      {:ok, %HTTPoison.Response{status_code: 404}} ->
        conn
        |> put_status(404)
        |> json(%{error_code: "404", reason_given: "Resource not found."})
      {:error, %HTTPoison.Error{reason: reason}} ->
        conn
        |> put_status(500)
        |> json(%{error_code: "500", reason_given: "None."})
    end

  do_something_else(params)

  final_conn
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 **(RuntimeError)预期的操作/ 2返回一个Plug.Conn,所有插件都必须接收一个连接(conn)并返回一个连接 - ** (RuntimeError) expected action/2 to return a Plug.Conn, all plugs must receive a connection (conn) and return a connection expected:action / 2返回Phoenix控制器中的Plug.Conn错误 - expected :action/2 to return a Plug.Conn error in Phoenix controller Elixir从控制器获取错误:预期动作/ 2返回Plug.Conn - Elixir getting error from controller: Expected action/2 to return a Plug.Conn 如何将请求参数填充到 Plug.Conn 连接中? - How to populate request parameters into a Plug.Conn connection? Phoenix:如何在控制台中获得conn%Plug.Conn {} - Phoenix: How to get conn %Plug.Conn{} in the console 从Plug.Conn获取HTTP_REFERRER - Get HTTP_REFERRER from Plug.Conn 来自 %Plug.Conn{} 远程 IP 的 Phoenix 解析请求主机 - Phoenix Resolve Request Host from %Plug.Conn{} remote ip Phoenix / Elixir:如何使用Plug.Test.conn()在测试连接中设置操作? - Phoenix/Elixir: How to set the action in a test connection with Plug.Test.conn()? Phoenix Framework的Plug.Conn中assign和put_session有什么区别? - What is the difference between assign and put_session in Plug.Conn of the Phoenix Framework? 如何使用Plug.Conn读取phoenix控制器中的小块数据 - How to read small chunks of data in phoenix controller, using Plug.Conn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM