elixir - (RuntimeError) expected action/2 to return a Plug.Conn, all plugs must receive a connection (conn) and return a connection -
in 1 of controllers i've got following code (excerpt):
case httpoison.get("https://*****.zendesk.com/api/v2/users/search.json?query=" <> clid, headers, [hackney: hackney]) {: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 run code, works fine, phoenix throws runtime exception:
** (exit) exception raised: ** (runtimeerror) expected action/2 return plug.conn, plugs must receive connection (conn) , return 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 doing wrong?
the stack trace telling controller action not returning plug.conn
struct. in elixir, result of last expression of function returned. @ last line of function , ensure returning result of case expression.
def(conn, params) final_conn = case httpoison.get("https://*****.zendesk.com/api/v2/users/search.json?query=" <> clid, headers, [hackney: hackney]) {: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
Comments
Post a Comment