mschae / cors_plug

An Elixir Plug to add CORS.

Home Page:https://hex.pm/packages/cors_plug

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No CORS headers embedded in Plug.ErrorHandler code path

junjizhi opened this issue · comments

My program was simply using Plug and wasn't using Phoenix.

  use Plug.Router
  use Plug.ErrorHandler
  use Sentry.Plug

  plug(CORSPlug,
    origin: &Router.get_origins/0
  )

  plug(Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json, CustomParser],
    pass: ["*/*"],
    json_decoder: Jason
  )

  def handle_errors(conn, _error_context) do
    send_resp(conn, conn.status, "")
  end

When the server crashes, it goes to handle_error and send a response back without CORS headers. I had to manually call CORSPlug.call and init to add the headers back.

Is this a bug? Or this is intended behavior? Do we have a better way to handle this?

Hey,

I’m pretty sure that no plugs work if one of them fails. Behind the scenes they will be mashed together to one big function to optimize runtime.

That’s why you use plugs that catch errors instead of pluging them. They actually just wrap everything in a try/catch blog.

In other words this behavior is expected.

@mschae Thanks for the clarification!