Software experiences errors. Even the best applications will encounter errors at some juncture. Therefore, every application should have some error-handling mechanisms in place.

Spring Boot provides a default Whitelabel error page as a component of its auto-configuration for error handling. Nonetheless, the expectation is that developers will create a custom error page that replaces the Whitelabel error page. In this article, you’ll learn how to customize the error page for your Spring Boot applications.

Whitelabel error page

Spring Boot’s Whitelabel Error Page

When a Spring Boot application encounters an error, it requests the/errorURL. If there is no view at this location it displays the Whitelabel error page:

The Whitelabel error page states the date and time of the error, along with its corresponding time zone. Additionally, it indicates the error type and its associated code. The Whitelabel page states thatthis is a 404 error(page not found). This is because the sample application has no mapping for the “/products” URL.

Customized error page

Most of the information presented on the Whitelabel error page is taken from specific error attributes. Spring Boot’s error view has access to the following error attributes:

Creating an Error Page With Thymeleaf

Your Spring Boot application should have a single error page stored within an “error” template. The extension of this template will vary depending on the template technology you decide to use. For example, if you opt for a Java Server Pages (JSP) template, the file name should beerror.jsp.

However, this sample Spring Boot application usesthe Thymeleaf template engine. So, the template name iserror.html. You should consistently place your error template in thetemplatefolder, under theresourcesdirectory with all your other template files.

The error.html File

The customized error page accomplishes several important tasks. It declares the occurrence of an error. Subsequently, it showcasesthe HTTP requestthat triggered the error. Furthermore, it supplies the user with the status code associated with the error. But if the user isn’t familiar with status codes, the page also explains the meaning of the code through the error attribute.

The final line of text presents the user with a message in the event of an exception. Then, the link at the end permits the user to navigate back to the home page. Theerror.htmlfile uses a CSS style sheet and two images to create the following view:

Keep Your Error Page User-Friendly

The primary purpose of the error page is to inform the user that a specific error has occurred. However, this error page is still an aspect of the application. Therefore, ensuring that the error page is also user-friendly is crucial.

This will mean opting to use the error attributes that communicate the error in a more uncomplicated way. So, you might choose to use the path attribute instead of the trace attribute, which is much more complex and contains details that the user doesn’t need to know.

You also don’t want to provide a random user with excessive information about the inner workings of your application, as this might compromise the application’s security.