(FunctionClauseError) - why I am getting this or how to fix it?

So the erro meassage is :

(exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in MyApiWeb.FallbackController.call/2

And the code where it is coming from is :

def create(conn, %{
  "description" => description,
  "email" => email,
  "source" => source,
  "subscriptionID" => subscriptionID
}) do
  with {:ok, %t{} = t} <- Stripe.Customer.create(%{description: description, email: email}) do
    conn
    |> IO.inspect(t)
  end
end

Thanks

I’m not sure what you try to bind to t in the with clause. But regardless what gets bound to it, its either an atom or a map/struct, while IO.inspect/2 expects keywords as its seconds argument (where t is used).

I am totally not sure though, if that is the problem though, since your error message complains about call/2 and you have shown only the create action.

To actually find your problem you’ll probably need to show more of your controller, as call/2 is the function that should dispatch the request to your actual action, so the problem is much earlier in the stack.

I am trying to use stripity stripe, and it is form their documentation, so I am not sure myself.
currently I only have this create action in my controller

There has to be more, some use or other stuff or there wouldn’t even be the call/2 function from the error message.

So could you please share the full controller?

sure and thanks

defmodule MyApiWeb.UserController do
  use MyApiWeb, :controller
  action_fallback(MyApiWeb.FallbackController)
  require Logger

  def create(conn, %{
        "description" => description,
        "email" => email,
        "source" => source,
        "subscriptionID" => subscriptionID
      }) do
    with {:ok, %t{} = t} <- Stripe.Customer.create(%{description: description, email: email}) do
      conn
      |> IO.inspect(t)
    end
  end
end

A fallback controller will be called when your actual action fails to return a proper conn.

As the with can’t match after my understanding (I’ve explained that above), you always return nil, which isnt a valid conn. So the fallback will be called. Why that doesn’t have a clause matching, we can’t say, because you haven’t shown it.

Also can you please share a link to the resources that gave you the example?

well I tried to puzzle it out from many, beause I could not find an example that uses striptiy stripe or elixir with stripe really.
https://hexdocs.pm/stripity_stripe/Stripe.Customer.html#content
also I am assuming that I am using this wrong but I just dont really understand that what the doc is trying to say

https://medium.com/@njwest/jwt-auth-with-an-elixir-on-phoenix-1-3-guardian-api-and-react-native-mobile-app-1bd00559ea51

I used this as my base, made it work earlier, so I just used it as a general guide, these were the main ones, but had some other ones too

as for my code the router:
defmodule MyApiWeb.Router do
use MyApiWeb, :router

  pipeline :api do
    plug(CORSPlug, origin: "*")
    plug(:accepts, ["json"])
  end

  scope "/api", MyApiWeb do
    pipe_through(:api)

    post("/users", UserController, :create)
    # options("/users", UserController, :create)
  end
end

Sorry I have to say, but I can’t find anything in the links that is like the snippet you posted.

So I have to ask again, where did you found the t = %t{} pattern? What are you trying to achieve by using it?

What is it you want to inspect in the withs body? t or conn?

Have you understood what with is used for?

Why do you use a fallback controller at all? Do you really need it? Do you understand what it is meant for?


PS: the router is probably unrelated, the error is raised from your fallback controller.

the fallback controller was auto generated, this t = %t{} my friend said it and what it tries to achieev is to get the created user.
I want ot inspect the created user so t in this case.
Maybe if you know an open source project that uses elixir with stripe could yo link it?
or just help me understand how to use the package?

Thank you

:wave:

t = %t{} seems rather pointless to me since, for example

iex(3)> t = %t{} = %User{}
%User{id: nil}
iex(4)> t
%User{id: nil}

here the intermediate value (from %t{} = %User{} such that t = User) is lost. If you want to pattern match on a user struct, it’s usually accomplished like this

iex(3)> %User{id: user_id} = %User{}

To better understand what goes wrong with

(exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in MyApiWeb.FallbackController.call/2

we would either need to see the implementation of MyApiWeb.FallbackController or at least what’s in t that causes the “happy path” in with to fail. Try this

def create(conn, %{
  "description" => description,
  "email" => email,
  "source" => source,
  "subscriptionID" => subscriptionID
}) do
  with {:ok, t} <- Stripe.Customer.create(%{description: description, email: email}) do
    conn # are you sure you don't want to `send_resp(conn, 200, [])` at least?
  else 
    unmatched ->
      IO.inspect(unmatched, label: "unmatched")
  end
end

I wasn’t in the need of a payment provider so far.

But please try to match only {:ok, user} and then do IO.inspect user; conn.

I have not yet seen this during my phoenix baby steps, are you using a 1.4 preview?

The fallback controller looks like this:

defmodule MyApiWeb.FallbackController do
  @moduledoc """
  Translates controller action results into valid `Plug.Conn` responses.

  See `Phoenix.Controller.action_fallback/1` for more details.
  """
  use MyApiWeb, :controller

  def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
    conn
    |> put_status(:unprocessable_entity)
    |> render(MyApiWeb.ChangesetView, "error.json", changeset: changeset)
  end

  def call(conn, {:error, :not_found}) do
    conn
    |> put_status(:not_found)
    |> render(MyApiWeb.ErrorView, :"404")
  end
end

tried this {:ok, user} and i removed the fallback controller and then it gave a new error :

 (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol String.Chars not implemented for %Stripe.Customer{account_balance: 0, business_vat_id: nil, created: 1532261740, currency: nil, default_source: nil, delinquent: false, description: "Monthly subscription", discount: nil, email: "fs@as.com", id: "cus_DHExEBEuX9Jks8", invoice_prefix: "3AB85AC", livemode: false, metadata: %{}, object: "customer", shipping: nil, sources: %Stripe.List{data: [], has_more: false, object: "list", total_count: 0, url: "/v1/customers/cus_DHExEBEuX9Jks8/sources"}, subscriptions: %Stripe.List{data: [], has_more: false, object: "list", total_count: 0, url: "/v1/customers/cus_DHExEBEuX9Jks8/subscriptions"}}

as for using the preview, i just installed phoneix a few days ago so I am not aware of using preview version.

Also I tried what khm idiot suggested and with that for the first try it went throug nicely but for the second one got the stripe user printed out and got the same fallback error

The original error says that no function head in MyApiWeb.FallbackController matches what’s returned from your create controller action. MyApiWeb.FallbackController, judging by your code snippet, can match either {:error, %Ecto.Changeset{} = changeset} or {:error, :not_found}, and nothing else. So if it receives, for example, %Stripe.User{}, it just doesn’t know what to do with it. You can add a new function which would match what’s returned from the create action and the error would disappear, but I doubt that’s what you actually want. I think the real problem is with the pattern in your with statement, {:ok, %t{} = t}.

Also I tried what khm idiot suggested and with that for the first try it went throug nicely but for the second one got the stripe user printed out and got the same fallback error

:+1:

But printed out where? If you mean IO.inspect, then for which label, unmatched? If so, then that’s your solution right there – fix your pattern in with to match on a stripe user so that it doesn’t fail.

Thank you