Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no function clause matching in anonymous fn/2 in Plug.Conn.merge_resp_headers/2 #41

Closed
zhhz opened this issue Aug 9, 2017 · 7 comments

Comments

@zhhz
Copy link

zhhz commented Aug 9, 2017

After upgrade to 1.4.0, my console spits the error:

Server: localhost:4000 (http)
Request: OPTIONS /graphql
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in anonymous fn/2 in Plug.Conn.merge_resp_headers/2
(plug) lib/plug/conn.ex:663: anonymous fn({"access-control-expose-headers", []}, [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "s83p1st550ks4i88vkahrdlopo2nk6am"}, {"vary", "Origin"}, {"access-control-allow-origin", "http://localhost:3000"}]) in Plug.Conn.merge_resp_headers/2
(elixir) lib/enum.ex:1811: Enum."-reduce/3-lists^foldl/2-0-"/3
(plug) lib/plug/conn.ex:663: Plug.Conn.merge_resp_headers/2
(cors_plug) lib/cors_plug.ex:33: CORSPlug.call/2

I'm very new to elixir, so am not sure if this related to this plug.

Thanks.

@zhhz
Copy link
Author

zhhz commented Aug 9, 2017

I could be wrong, is that related to this commit?

35fb923

@mschae
Copy link
Owner

mschae commented Aug 16, 2017

Hey @zhhz sorry for the late reply.

It looks like you're trying to pass an empty list to access-control-expose-headers. That's illegal and therefore it's correct that you get the error you're seeing.

Can you please post a code snipper here showing how you configured the plug?

Thanks,
Michael

@slashmili
Copy link
Contributor

@mschae I have this config in my endpoint:

plug CORSPlug, origin: ["https://localhost:5000", "http://localhost:5000"], headers: ["Content-Type", "x-csrf-token"]

and most of my tests are failing this is the error message:

  3) test logins user with correct password (Interface.API.LoginControllerTest)
     test/controllers/api/login_controller_test.exs:13
     ** (FunctionClauseError) no function clause matching in anonymous fn/2 in Plug.Conn.merge_resp_headers/2

     The following arguments were given to anonymous fn/2 in Plug.Conn.merge_resp_headers/2:

         # 1
         {"access-control-expose-headers", []}

         # 2
         [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "i7s0iaroobcmmpivhtq7l42csh74mrq2"}, {"vary", "Origin"}, {"access-control-allow-origin", "null"}]

     code: conn = post conn, login_path(conn, :login), %{email: user.email, password: @valid_password}
     stacktrace:
       (plug) lib/plug/conn.ex:663: anonymous fn/2 in Plug.Conn.merge_resp_headers/2
       (elixir) lib/enum.ex:1826: Enum."-reduce/3-lists^foldl/2-0-"/3
       (plug) lib/plug/conn.ex:663: Plug.Conn.merge_resp_headers/2
       (cors_plug) lib/cors_plug.ex:33: CORSPlug.call/2
       (interface) lib/interface/endpoint.ex:19: Interface.Endpoint.phoenix_pipeline/1
       (interface) lib/interface/endpoint.ex:19: Interface.Endpoint.call/2
       (phoenix) lib/phoenix/test/conn_test.ex:224: Phoenix.ConnTest.dispatch/5
       test/controllers/api/login_controller_test.exs:14: (test)

@mschae
Copy link
Owner

mschae commented Oct 10, 2017

@slashmili Sorry to hear it. I'm a bit stumped, it looks like the expose headers option is a list. This line however should prohibit that:
https://github.com/mschae/cors_plug/blob/master/lib/cors_plug.ex#L21

The only real explanation I have at this point is that the plug wasn't properly initiated.

Can you create an example project with a failing test somewhere?

Thanks

@houmanka
Copy link

houmanka commented Dec 31, 2017

@slashmili I encounter the same issue.
So I copied the default into the endpoint.ex

  plug CORSPlug, [
      origin:      "*",
      credentials: true,
      max_age:     1728000,
      headers:     ["Authorization", "Content-Type", "Accept", "Origin",
                    "User-Agent", "DNT","Cache-Control", "X-Mx-ReqToken",
                    "Keep-Alive", "X-Requested-With", "If-Modified-Since",
                    "X-CSRF-Token", "company"],
      expose:      [],
      methods:     ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
    ]
    plug Web.Router

and everything started to work fine

BTW, Same error wasn't because of the company

This was the original

  plug CORSPlug, headers: ["*"]
  plug Web.Router

Then frontend developer notified me that the DELETE method gets the CORS error. So I checked it and find out that he is sending OPTIONS instead of DELETE. However it is not passing the CORS so I added the default to the config as above and everything started to work fine ( Strange!! )

@slashmili
Copy link
Contributor

I somehow managed to fix the problem, I forgot to report it back. I don't recall the details but I guess I did something wrong 😆

@mschae
Copy link
Owner

mschae commented Feb 27, 2018

I'll close this for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants