CTA BG
Blog
Custom 404 Handling in a Blacklight App

Custom 404 Handling in a Blacklight App

404 not found

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."

Blacklight apps have a default "404.html" page, but any time we can provide branded content, we should. :)

This can be achieved in two ways:

  • A custom page that the "/404" endpoint would route to
  • A custom page that users see for an invalid search result

"/404" route

app/controllers/errors_controller.rb

-- This is the controller that sets up our status and renders the correct page

# frozen_string_literal: true
class ErrorsController < ApplicationController
  def not_found
    render status: 404
  end
end

app/views/errors/not_found.html.erb<

  • No need for the div if additional styling isn't needed
  • This is the same text that shows on the default "404.html" page, but can be adjusted as desired
<div class='record-not-found'>
    <h1>The page you were looking for doesn't exist.</h1>
    <p>You may have mistyped the address or the page may have moved</p>
    <p>If you are the application owner check the logs for more information.</p>
</div>

config/routes.rb -- This will map the "/404" route to our custom page above

# Custom error pages
get '/404', to: 'errors#not_found', via: :all

spec/requests/errors_spec.rb

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Errors', type: :request do
  describe ':not found' do
    before(:all) { get '/404' }

    it 'have an http status of 404' do
      expect(response).to have_http_status(:not_found)
    end

    it 'redirect to the custom not_found error page' do
      expect(response.body).to include("The page you were looking for doesn't exist.")
    end
  end
end
NOTE: the default "public/404.html" file must be deleted for the above to take effect

Invalid search result

app/controllers/catalog_controller.rb

  • If "/catalog/alpha" is an invalid search, it will trigger the exception handling below
  • This code is telling the app to render the "record_not_found" file in "views/catalog" folder, along with a 404 status
rescue_from Blacklight::Exceptions::RecordNotFound do
  render 'record_not_found', status: 404
end

app/views/catalog/record_not_found.html.erb

  • Again, no need for the div if additional styling isn't needed
<div class='record-not-found'>
  <h1>The page you were looking for doesn't exist.</h1>
  <p>You may have mistyped the address or provided an invalid Object ID (&lt;%= params[:id] %>)</p>
</div>

spec/requests/catalog_controller_request_spec.rb

describe 'responds to the Blacklight::Exceptions::RecordNotFound exception' do
  it 'redirects to a custom 404 page' do
    params = { id: 'alpha' }
    get "/catalog/#{params[:id]}"

    expect(response.status).to eq(404)
    expect(response.body).to include("The page you were looking for doesn't exist")
    expect(response.body).to include("You may have mistyped the address or provided an invalid Object ID (#{params[:id]})")
  end
end
NOTE: there's no need for routing because the user remains at the route they tried to enter
Alisha Evans
Alisha Evans