Error on error branch of with statement

I am getting an error form the err branch of the with statement listed below. The error message (below) is “argument error”, but I can’t see what’s wrong.

In user_controller.ex:

  def create(conn, %{"user" => user_params}) do
    user_params |> IO.inspect(label: "user_params")
    with {:ok, username} <- Query.username_is_available(user_params["username"]),
         {:ok, email} <- Query.email_is_available(user_params["email"]),
         {:ok, %User{} = user} <- UserSpace.create_user(user_params) do
              |> put_status(:created)
              |> put_resp_header("location", user_path(conn, :show, user))
              |> render("show.json", user: user)
      err -> render("error.json", error: "duplicate email or username")

In user_view.ex:

def render("error.json", %{error: message}) do
    %{ error: message }

Error message:

iex(8)> [debug] Preflight CORS request from Origin "http://localhost:8080" is allowed
[debug] Simple CORS request from Origin "http://localhost:8080" is allowed
[info] POST /api/users/
user_params: %{
  "email" => "",
  "firstname" => "eta",
  "password" => "lobo4795",
  "username" => "eta"
[debug] Processing with BookListWeb.UserController.create/2
  Parameters: %{"user" => %{"email" => "", "firstname" => "eta", "password" => "[FILTERED]", "username" => "eta"}}
  Pipelines: []
[debug] QUERY OK source="users" db=0.4ms
SELECT u0."id", u0."email", u0."username", u0."firstname", u0."lastname", u0."password_hash", u0."admin", u0."blurb", u0."public", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."username" = $1) ["eta"]
[info] Sent 500 in 20ms
[error] #PID<0.594.0> running BookListWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: POST /api/users/
** (exit) an exception was raised:
    ** (ArgumentError) argument error
        :erlang.apply("error.json", :private, [])

@jxxcarlson it appears you’re missing the connection in your render/3. I believe you’ll want something like this:

render(conn, "error.json", error: "duplicate email or username")
1 Like

This is syntactic sugar for passing a keyword-list as the last argument.

This tries to match on a map, not a keyword-list.

1 Like

Oops! I see now. Thankyou!!

1 Like


You’re most welcome :grin:

1 Like