Issues with handling Plug Conn

Error:

17:04:51.464 [error] #PID<0.488.0> running MyWeb.Router (connection #PID<0.487.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /ping
** (exit) an exception was raised:
    ** (RuntimeError) expected dispatch/2 to return a Plug.Conn, all plugs must receive a connection (conn) and return a connection

My Endpoint:

defmodule MyWeb.Endpoint do
  use Plug.Builder

  Plug.Head

  plug Corsica,
    max_age: 600,
    origins: "*",
    allow_headers: ["accept", "content-type", "authorization"],
    allow_methods: ["GET", "POST"],
    allow_credentials: true,
    log: [rejected: :error, invalid: :warn, accepted: :debug]
end

My Router:

defmodule MyWeb.Router do
  use Plug.Router
  require Ecto.Query

  plug(:match)
  plug(:dispatch)
  plug(MyWeb.Endpoint)

  get "/" do
    send_resp(conn, 200, "hi there")
  end
end

please help me :confused:

You don’t have any corresponding /api route in MyWeb.Router; this doesn’t seem related to CORS at all.

right I was using the wrong url, but the connection of plug is still an issue

Server: localhost:4000 (http)
Request: GET /ping
** (exit) an exception was raised:
** (RuntimeError) expected dispatch/2 to return a Plug.Conn, all plugs must receive a connection (conn) and return a connection

do you know why?

What does the code that matches /ping look like?

Plugs match in the order they are defined:

  plug(:match)
  plug(:dispatch)
  plug(MyWeb.Endpoint)

This seems like the wrong order. This will run your get BEFORE doing anything in Endpoint.

Are you using Phoenix or just plug. If just plug can you elaborate on how you’re using the term Endpoint?

I would use plug MyWeb.Router in the endpoint instead of putting the endpoint in the router you do now :slight_smile:

So Switch what I have right now?

im using plug. wdym how using term endpoint? MyWeb.Endpoint is just a file that is handling CORS with corsica

get "/ping" do
    response = HTTPoison.get!(@base_url)
    send_resp(conn, 200, response.body)
  end