Custom Error Pages

Skill required
Basic Web programming

In this tutorial, we are running a Single Page Application (SPA) with some API-based custom business logic hosted in a Docker container.

network diagram

We will configure the access rules such that all URL starting with /api are forwarded to the business logic container, while all other URLs are served directly by the Single Page App.

URL path Access rule Forward
/api authenticated Yes
/ any No

What could go wrong?

There are obviously a multiple of errors that can result from running any application on the Internet. Here we will focus on the two most common errors that can result from deploying a Single Page Application and Business logic API server on the DjaoDjin hosting platform.

  1. The SPA does not load, or it loads on some URL paths, but not others.
  2. The Docker container running the API business logic is not responding.

The SPA does not load

DjaoApp is designed to serve a few public URLs to quickly get started (ex: home page, login page, pricing page). A Single Page App (SPA) typically has a index.html that loads the JavaScript code, that in turns create the Document Object Model (DOM).

You will need to copy that SPA index.html into the DjaoApp theme, i.e. templates/index.html or public/index.html (see structure of a theme package). Since here we do not need the DjaoApp Gateway to inject variables into the page, we copy the SPA index.html into public/index.html.

Now if we browse to https://mysite.djaoapp.com, our SPA is loading. DjaoApp login page is still functional. Since we are interested to run authentication through the SPA instead of customizing the login theme template, we are making a copy of the SPA index.html as public/login.html. We make as many copies as there are DjaoApp URL paths we want to override with the SPA code instead of using the default DjaoApp logic.

Here the list of all DjaoApp public URLs and associated template.

URL path Public Override Dynamic template Override Description
/activate/ public/activate/index.html templates/accounts/activate/index.html Starts authentication workflow
/activate/{verification_key}/ N/A templates/accounts/activate/verification_key.html Landing page for links sent in an activation e-mail
/contact/ public/contact.html templates/contact.html The classic "Contact us" page
/legal/ public/legal/index.html templates/saas/legal/index.html Lists legal documents (ex: terms of service, security policy)
/legal/{agreement}/ N/A templates/saas/legal/agreement.html Shows a single agreement (or policy) document
/login/ public/login.html templates/accounts/login.html The classic username and password authentication page
/pricing/ public/pricing.html templates/saas/pricing.html The classic pricing page
/recover/ public/recover.html templates/accounts/recover.html The classic forgotten password page
/redeem/ public/redeem.html templates/saas/redeem.html Landing page to apply a discount code
/register/ public/register.html templates/accounts/register.html The classic registration page
/reset/{verification_key}/ N/A templates/accounts/reset.html Landing page for links sent in an recover password e-mail

Once we override all DjaoApp public end points, there remains only two scenarios under which the SPA does not load for a visitor that hasn't been authenticated:

  1. We have other public URLs that our application considers valid. In which case we copy the SPA index.html to a matching file in the DjaoApp theme package. For example, our SPA serves the '/club/promo/' URL path to anonymous visitors. We thus copy the SPA index.html to public/club/promo.html.
  2. A bot is enumerating bogus URLs, or a visitor has made a typo in the URL path. When that happens, DjaoApp will serve templates/404.html. If we really want the SPA to be involved then as well, we copy the SPA index.html to templates/404.html.

The Docker container is not responding

Once a user has been authenticated, and started interacting with the SPA, a few API calls to the business logic container will be triggered at some point.

Since the Docker container is not running yet, we will get 502 Bad Gateway error messages.

To customize those error messages, we create a public/50x.html asset. Note that the 50x.html file goes in the public/ directory, and not the templates/ directory because it is served by the Content Delivery Network (CDN).

Et voila!

Review

Create a directory structure with the following files (as copies of your SPA index.html)

  • public/
    • index.html
    • activate/index.html
    • contact.html
    • legal/index.html
    • login.html
    • pricing.html
    • recover.html
    • redeem.html
    • register.html
    • 404.html
    • 50x.html
  • templates/
    • accounts/activate/verification_key.html
    • saas/legal/agreement.html
    • accounts/reset.html

Upload the custom theme package to your Website hosted on DjaoDjin.

bash
djd upload public/ templates/

To go further, find more information on Setting up a DevOps workflow.


Need help?
Contact us
Curious how it is built?
Visit us on GitHub