I am a Phoenix/Elixir beginner and am trying to write an API to allow users to sign up in my application.
The API endpoint works as expected unless I try to set the HTTP status code of the response. When I include lines A, B and C (indicated in the code below), I get a FunctionClauseError
with the message no function clause matching in :cowboy_req.status/1
.
The complete error message is as follows:
[error] #PID<0.344.0> running App.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /api/user/
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in :cowboy_req.status/1
(cowboy) src/cowboy_req.erl:1272: :cowboy_req.status(451)
(cowboy) src/cowboy_req.erl:1202: :cowboy_req.response/6
(cowboy) src/cowboy_req.erl:933: :cowboy_req.reply_no_compress/8
(cowboy) src/cowboy_req.erl:888: :cowboy_req.reply/4
(plug) lib/plug/adapters/cowboy/conn.ex:34: Plug.Adapters.Cowboy.Conn.send_resp/4
(plug) lib/plug/conn.ex:356: Plug.Conn.send_resp/1
(app) web/controllers/user_controller.ex:1: App.UserController.action/2
(app) web/controllers/user_controller.ex:1: App.UserController.phoenix_controller_app/2
(app) lib/app/endpoint.ex:1: App.Endpoint.instrument/4
(app) lib/phoenix/router.ex:261: App.Router.dispatch/2
(app) web/router.ex:1: App.Router.do_call/2
(app) lib/app/endpoint.ex:1: App.Endpoint.phoenix_app/1
(app) lib/plug/debugger.ex:122: App.Endpoint."call (overridable 3)"/2
(app) lib/app/endpoint.ex:1: App.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
My code is as follows:
defmodule App.UserController do
use App.Web, :controller
import Ecto.Changeset
alias App.User
alias App.Session
def create(conn, params) do
changeset = User.changeset(%User{}, params)
case Repo.insert(changeset) do
{:ok, _user} ->
email = get_field(changeset, :email)
password = get_field(changeset, :password)
# Log on user upon sign up
session_changeset = Session.changeset(%Session{
email: email,
password: password
})
result = Repo.insert(session_changeset)
case result do
{:ok, session} ->
conn
|> put_resp_cookie("SID", session.session_id)
|> put_status(201) # line A
|> render("signup.json", data: %{
changeset: changeset
})
{:error, changeset} ->
conn
|> put_status(251) # line B
|> render("signup.json", data: %{
changeset: changeset
})
end
{:error, changeset} ->
conn
|> put_status(451) # line C
|> render("signup.json", data: %{
changeset: changeset
})
end
end
end
Why does this happen and where am I going wrong?