Extracting controller and action from Plug.conn

Using Phoenix/Plug is there a good way to parse out which controller and action an error occurred in?
While the controller and action is present in the Plug.Conn struct normally.

private: %{Example.Router => {[], %{}}, :phoenix_action => :show,
   :phoenix_controller => Example.HealthController,
   :phoenix_endpoint => Example.Endpoint, :phoenix_format => "html",
   :phoenix_layout => {Example.LayoutView, :app}, :phoenix_pipelines => [:ctl],
   :phoenix_route => #Function<18.6883228/1 in Example.Router.match_route/4>,
   :phoenix_router => Example.Router, :phoenix_view => Example.HealthView,
   :plug_session_fetch => #Function<1.54110061/1 in Plug.Session.fetch_session/1>},

Once it’s handled by the ErrorHandler plug those fields are dropped.

private: %{Example.Router => {[], %{}}, :phoenix_endpoint => Example.Endpoint,
  :phoenix_format => "html", :phoenix_pipelines => [:ctl],
  :phoenix_route => #Function<18.105996037/1 in Example.Router.match_route/4>,
  :phoenix_router => Example.Router,
  :plug_session_fetch => #Function<1.54110061/1 in Plug.Session.fetch_session/1>},

Any other ideas?

5 Likes

You can use Phoenix.Controller.action_name(conn)

1 Like

If controller module is not available, I don’t believe the action will either. To extract either the plug needs to be located in the right place in the request chain, after the router plug. See Is there a way to insert plugs after the router but in front of the controller actions?