GitHub Star 1.6k

Error Pages

Hologram shows an error page when middleware or a page's init/3 ends a page request with an error status and no body, or when the request raises an exception. Define one with use Hologram.ErrorPage and name its status with the status/1 macro:

defmodule MyApp.NotFoundPage do
  use Hologram.ErrorPage

  status :not_found

  layout MyApp.MainLayout

  def template do
    ~HOLO"""
    <h1>Error {@status}</h1>
    <p>There's nothing at this address.</p>
    """
  end
end

With this page in the app, put_status(server, :not_found) in any page's middleware or init/3 shows it. The response keeps the 404 status, and the address bar keeps the URL the visitor asked for. On navigation, the error page opens without reloading the document.

Statuses

status/1 takes a code, an alias, a list of those, or :any:

:any covers every error status that no other error page names. The error statuses are 400 to 599, and the Middleware documentation lists their aliases. The build fails when an error page names no status, calls status/1 twice or names a status outside that range, and when two error pages name the same one.

Differences from Other Pages

An error page has a layout, state, init/3, middleware, actions and commands, like any page. It differs in three ways:

What Carries Over from the Requested Page

When middleware or a page's init/3 set the status, the error page's middleware and init/3 continue with the server struct the requested page left. The stash, session, cookies, user ID and response headers are all there, so middleware can stash the reason for a denial and the error page can read it. The requested page's middleware doesn't run again.

Some of it is left out:

Hologram sends the broadcasts the requested page queued once the error page has rendered, and drops them if the error page raises.

Exceptions

An exception in a page request shows the error page that covers the status 500, either by naming it or with status :any. Hologram catches exceptions in middleware, in every init/3 and template on the page, and while it builds the response, for example when the state holds a value it can't send to the browser. Throws and exits count too.

The status is always 500, even when the exception declares another one. Repo.get! raises Ecto.NoResultsError, which declares 404, but the visitor gets the 500 error page. To show the 404 one, use Repo.get and call put_status(server, :not_found) when it returns nil.

The error page starts from the server struct the request started with. Hologram drops the stash, session changes, cookies, user ID, response headers, broadcasts and subscriptions of the failed request. A visitor who was logged in before the request still is. The error page gets the status and nothing about the exception.

After sending the error page, Hologram raises the exception again. It ends up in your server log, and error trackers that wrap the endpoint get it. But the response is already out by then, so handle_errors/2 of a Plug.ErrorHandler around Hologram doesn't run, and Phoenix doesn't emit its [:phoenix, :error_rendered] telemetry event.

If that error page raises too, Hologram logs its exception and raises the original one without sending anything. The visitor then gets what they'd get in an app with no error page for 500, such as Phoenix's own error response. An exception in an error page shown for a status from put_status doesn't show another error page either.

In dev and test, an overlay on top of the error page shows the exception's message and stacktrace. Close it to see the error page. The client_error_overlay setting turns it off (see Configuration). No other environment sends the exception to the browser.

When No Error Page Is Shown

Hologram sends the response as it is when it has a body (an empty string counts as one), when no error page covers its status, and when a command's middleware set the status.

URLs that no route matches don't show an error page, and neither do these exceptions:

Back and Forward

Back and forward restore an error page the way the visitor left it, like any page. When Hologram can't restore it, it asks for the requested page again, so a visitor who can see that page by then gets it.

Sponsored by
Curiosum
Main sponsor
Erlang Ecosystem Foundation
Milestone sponsor
Evo Store
Performance sponsor
Sruplex
Innovation partner
Previous
← Middleware