top of page

Understanding the 404 Not Found Error: How to Customize Your Response

  • Alisha Evans
  • Apr 13, 2021
  • 3 min read

What is a 404 Error?


The meaning of a 404 status error (according to Wikipedia) is: "The requested resource could not be found but may be available in the future. Subsequent requests by the client are permissible."


When users encounter a 404 error, it can be frustrating. However, this is an opportunity to enhance user experience. Blacklight apps have a default "404.html" page, but any time we can provide branded content, we should. :)


Customizing the "/404" Route


To create a more engaging experience, you can customize the "/404" route. This involves creating a specific page that users will see when they hit a 404 error. Here’s how to do it:


Step 1: Set Up the Controller


In your application, navigate to `app/controllers/errors_controller.rb`. This is the controller that sets up our status and renders the correct page.


Step 2: Create the Custom View


Next, create a custom view for the 404 error. Go to `app/views/errors/not_found.html.erb`. You may notice that there’s no need for the div if additional styling isn't needed. This page can contain the same text that shows on the default "404.html" page, but feel free to adjust it as desired.


Step 3: Map the Route


In the `config/routes.rb` file, you will need to map the "/404" route to your custom page. This ensures that users are directed to your branded content when they encounter a 404 error.


Step 4: Testing Your Implementation


To ensure everything works correctly, check the `spec/requests/errors_spec.rb` file. This is where you can write tests to confirm that your custom 404 page is displayed as expected.


NOTE: The default "public/404.html" file must be deleted for the above to take effect.

Handling Invalid Search Results


Another common scenario is when users perform an invalid search. This can also trigger a 404 error. Here’s how to handle it:


Step 1: Update the Catalog Controller


In `app/controllers/catalog_controller.rb`, you will find the logic that handles searches. If "/catalog/alpha" is an invalid search, it will trigger the exception handling below. This code tells the app to render the "record_not_found" file located in the "views/catalog" folder, along with a 404 status.


Step 2: Create the Record Not Found View


Next, create the view for invalid search results. Navigate to `app/views/catalog/record_not_found.html.erb`. Similar to the 404 page, there’s no need for the div if additional styling isn't needed.


Step 3: Testing Invalid Searches


You can also test this functionality in the `spec/requests/catalog_controller_request_spec.rb` file.


NOTE: There's no need for routing because the user remains at the route they tried to enter.

Best Practices for 404 Pages


Keep It Simple


A clean and simple design can help users navigate away from the error page. Include a search bar or links to popular pages on your site.


Provide Helpful Links


Consider adding links to your homepage or other relevant sections of your site. This can guide users back to useful content.


Use Humor or Branding


If appropriate, use humor or your brand's voice to lighten the mood. A friendly message can make the experience less frustrating.


Conclusion


Customizing your 404 error page and handling invalid search results effectively can greatly improve user experience. By following the steps outlined above, you can create a branded experience that keeps users engaged, even when they encounter errors.


For more information on enhancing user experience, check out this resource.


Remember, a well-designed 404 page can turn a frustrating experience into a positive one!

bottom of page