I have a route at the end to match all routes and manually call a controller for rendering (with layout) a 404 html.heex in live mode:
match :*, "/*path", MyProject.Error404Controller, :call
With the modules:
defmodule MyProject.Error404Controller do
use MyProject, :controller
import Phoenix.LiveView.Controller
def call(conn, _) do
conn
|> put_status(:not_found)
|> live_render(MyProject.Error404Live, session: %{"path" => conn.request_path})
end
end
defmodule MyProject.Error404Live do
use MyProject, :live_view
def render(assigns) do
Phoenix.View.render(MyProject.ErrorView, "404.html", assigns)
end
def mount(_params, %{"path" => path}, socket) do
{:ok, assign(socket, :path, path)}
end
# I also need handle_params which triggers the error
end
Problem is that I need handle_params function to initialize some internal stuff, but since the liveview was not started via live/3 macro, I am getting:
cannot invoke handle_params/3 on MyProject.Error404Live because it is not mounted nor accessed through the router live/3 macro
As I understand it, this seems a phoenix current limitation which according to Jose Valim this limitation might be removed in the future.
I’ve seen similar posts regarding this and there are suggestions that I enable a new route like /404/*path called using live/3 macro and just let the error redirect. While this might work, it will change URL bar which will confuse the user.
Is there any way to accomplish a full 404 error page with layouts enabled and in full liveview mode? (This is because inside the layout I am using @socket instead of @conn.
Thanks.






















