簡體   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

在我的一個控制器中,我有以下代碼(摘錄):

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

當我運行代碼時,它工作正常,但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

我究竟做錯了什么?

堆棧跟蹤告訴您控制器操作未返回Plug.Conn結構。 在Elixir中,返回函數最后一個表達式的結果。 查看函數的最后一行並確保它返回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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM