How do I show my error templates?

I created the 404.html.eex and 500.html.eex templates which seem to be automatically correctly displayed when a 404 or 500 error occurs by the Phoenix framework.

But how can I manually display these templates in some part of my code?

E.g. how should I transform this code, which currently just sends the “not found” text to the browser:
conn |> put_status(404) |> text("not found") |> halt()
to show my custom 404 template?

I think you need to use the app error_view with its render function.

Here is a helper that you could call for the purpose:

  import Plug.Conn
  import Phoenix.Controller

  def render_error(conn, status) do
    |> put_status(status)
    |> put_layout(false)
    |> put_view(DemoWeb.ErrorView)
    |> render(:"#{status}")
    |> halt()

Of course for each status you have to define status.html.eex in templates/error folder and its render function in the ErrorView. But I think you’ve already done that for 404 and 500 status, otherwise they won’t be automatically displayed as you said.