Phoenix: conflicting behaviours found. function call/2 is required by Plug and Plug

Created on 15 Feb 2018  路  7Comments  路  Source: phoenixframework/phoenix

Environment

  • Elixir version (elixir -v): 1.6.0
  • Phoenix version (mix deps): 1.3.0
  • NodeJS version (node -v): v9.3.0
  • NPM version (npm -v): 5.5.1
  • Operating system: CentOS 6.9

Expected behavior

I did not get these warnings

Actual behavior

I get this warning:

warning: conflicting behaviours found. function init/1 is required by Plug and Plug (in module Red9.Authorization.Admin)
  lib/red9/authorization.ex:2

warning: conflicting behaviours found. function call/2 is required by Plug and Plug (in module Red9.Authorization.Admin)
  lib/red9/authorization.ex:2

warning: conflicting behaviours found. function init/1 is required by Plug and Plug (in module Red9.Authorization.Client)
  lib/red9/authorization.ex:49

warning: conflicting behaviours found. function call/2 is required by Plug and Plug (in module Red9.Authorization.Client)
  lib/red9/authorization.ex:49

warning: conflicting behaviours found. function init/1 is required by Plug and Plug (in module Red9.Authorization.Application)
  lib/red9/authorization.ex:108

warning: conflicting behaviours found. function call/2 is required by Plug and Plug (in module Red9.Authorization.Application)
  lib/red9/authorization.ex:108

My code:

defmodule Red9.Authorization.Admin do
        @behaviour Plug
        use Plug.Builder
        import Ecto.Query
        alias Red9.Repo

    @empty_key "00000000-0000-0000-0000-000000000000"


        def init(opts) do
                opts
        end
        def call(conn, _opts) do
                c_k = conn |> get_req_header("client-key") |> List.first
                ck = case c_k do
                        nil ->
                                (conn.query_params |> Map.get("client-key")) || @empty_key
                        data ->
                                data
                end
                admin_user = Red9.User
                                |> where(client_key: ^ck)
                                |> where(is_admin: true)
                                |> where(is_active: true)
                                #|> Repo.aggregate(:count, :id)
                                |> Repo.one
                conn = case admin_user do
                        nil ->
                                conn
                |> put_resp_content_type("application/json")
                |> send_resp(403, (%{
                      error: "not allowed",
                      errcode: "SVC-RED9-001",
                      description: "invalid client-key for restricted admin api"
                }
                ) |> Poison.encode!)
                                |> halt()
                        admin ->
                                conn
                                |> assign(:admin, admin)
                                |> assign(:client, admin)
                end
                conn
        end
end

All 7 comments

use Plug.Builder will automatically set the @behaviour Plug, so your use is duplicated. Remove your @behaviour Plug line and it will fix your warnings :)

Closing since this is not an issue with Phoenix, or Plug, but @josevalim may want to take a peek to see if the warning could be improved in such cases to make it more obvious what is happening. Thanks!

By removing @behaviour Plug still I get warnings. @chrismccord

You need to remove use Plug.Builder since you are not building multiple plugs. Just @behaviour Plug should remain.

To clarify, Plug.Builder handles the call implementation for you as long as you define multiple calls to plug. Since you are implementing call/2 yourself, then you are not using Plug.Builder, therefore that's the one you should remove.

@chrismccord My bad, I forgot to remove one. Thanks.
@josevalim Thanks. It did solve the problem.

@josevalim btw, I have to use Plug.Builder instead of @behaviour Plug. Because I get this error:

undefined function get_req_header/2

@ourway this function is defined in Plug.Conn, it isn't immediately available. You need to import Plug.Conn first, or call it as Plug.Conn.get_req_header.

Note, use Plug.Builder imports Plug.Conn for you, that's why it worked before.

Was this page helpful?
0 / 5 - 0 ratings