[英](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.