How to render an ErrorView, without FallbackController?

Trying to render an error view:

 # in a controller

  def test1(conn, params) do
    case Repo.get_by(MyModel, title: params["title"]) do
      nil ->
        put_status(conn, 403)
        |> put_layout(false)
        |> render(MyAppWeb.ErrorView, "404.html")


# in the config:

debug_errors: false,

Output:

Internal server error

Why? No idea.

And a 2nd question: How can render an error html template with no layout without having to say put_layout(false) from a controller? Namely, I want to set “nil” layout from within ErrorView.

You should check your log for the details.

But also setup your 500 error handler. :slight_smile:

Ok, that’s fixed. My 2nd question remains.

You can build your own render error function if You want to dry your code.

def test1(conn, params) do
    case Repo.get_by(MyModel, title: params["title"]) do
      nil -> render_error(conn)
...

defp render_error(conn) do
  conn
  |> put_status(403)
  |> put_layout(false)
  |> render(MyAppWeb.ErrorView, "404.html")
end
2 Likes

What’s unclear?

How can render an error html template with no layout without having to say put_layout(false) from a controller? Namely, I want to set “nil” layout from within ErrorView.

No You can’t.

1 Like