tmariaz
tmariaz

Reputation: 57

no function clause matching in Keyword.get/3

I’m new to elixir and when I run my api it throws an error. Not sure what is wrong.

UserController

def index(conn, params, _current_user, _params) do
    page = Accounts.list_all_users(params)

    conn
    |> Scrivener.Headers.paginate(page)
    |> render("index.json", all_users: page.entries)
end

Error

[info] GET /api/manage/all_users
[debug] Processing with Pxf.Web.Admin.UserController.index/2
  Parameters: %{}
  Pipelines: [:api]
[info] Sent 500 in 278ms
[error] #PID<0.1130.0> running Pxf.Web.Endpoint (connection #PID<0.1129.0>, stream id 1) terminated
Server: localhost:8000 (http)
Request: GET /api/manage/all_users
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in Keyword.get/3
        (elixir 1.14.2) lib/keyword.ex:388: Keyword.get(:default, :key, nil)
        (guardian 2.3.1) lib/guardian/plug.ex:373: Guardian.Plug.fetch_key/2
        (guardian 2.3.1) lib/guardian/plug.ex:141: Guardian.Plug.current_resource/2
        (pxf 0.1.0) lib/pxf/web/controllers/load_resource.ex:30: Pxf.Plug.LoadResource.call/2
        (pxf 0.1.0) Pxf.Router.api/2
        (pxf 0.1.0) lib/pxf/web/router.ex:1: Pxf.Router.__pipe_through1__/1
        (phoenix 1.6.15) lib/phoenix/router.ex:346: Phoenix.Router.__call__/2
        (pxf 0.1.0) lib/plug/error_handler.ex:80: Pxf.Router."call (overridable 3)"/2
        (pxf 0.1.0) lib/pxf/web/router.ex:1: Pxf.Router.call/2
        (pxf 0.1.0) lib/pxf/web/endpoint.ex:1: Pxf.Web.Endpoint.plug_builder_call/2
        (pxf 0.1.0) lib/pxf/web/endpoint.ex:1: Pxf.Web.Endpoint."call (overridable 3)"/2
        (pxf 0.1.0) lib/pxf/web/endpoint.ex:1: Pxf.Web.Endpoint.call/2
        (phoenix 1.6.15) lib/phoenix/endpoint/cowboy2_handler.ex:54: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy 2.9.0) /../dev/pxf-phoenix-v1.4/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
        (cowboy 2.9.0) /../dev/pxf-phoenix-v1.4/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
        (cowboy 2.9.0) /../dev/pxf-phoenix-v1.4/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
        (stdlib 4.1.1) proc_lib.erl:240: :proc_lib.init_p_do_apply/3

Edits api pipeline in router

  pipeline :api do
    plug(:accepts, ["json", :images])
    plug(VerifyHeader, realm: "Bearer")
    plug(LoadResource)
    plug(:log_ip)
  end

load_resource.ex (line 30-42)


case Guardian.Plug.current_resource(conn, key) do
      nil ->
        case Guardian.Plug.claims(conn, key) do
          {:ok, claims} ->
            claims |> load_resource(opts) |> put_current_resource(conn, key)

          {:error, _} ->
               Guardian.Plug.set_current_resource(conn, nil, key)
        end

      _ ->
        conn
    end

Also I'm receiving "Something went wrong" error when the api is called with 500 Internal Server Error.

Not sure how to debug to find out what is going. Please need some help on this.

thanks

Upvotes: 0

Views: 214

Answers (1)

Denis Tataurov
Denis Tataurov

Reputation: 193

Looking at the code of a custom LoadResource you should send a Keyword as the second argument (as Everett said). I'd start with changing this

case Guardian.Plug.current_resource(conn, key) do

to

case Guardian.Plug.current_resource(conn, [key: key]) do

And then proceed with further errors if there's any.

Not sure what you're trying to achieve as it's hard to guess by the code fragments but there is a Guardian.Plug.LoadResource out of the box. Perhaps it will be a better option for you to start instead of a custom LoadResource module.

Upvotes: 0

Related Questions