chore: generic customized error page

This commit is contained in:
Colin Darie 2024-04-08 23:36:18 +02:00
parent 5d23b37f59
commit 5b98fd7c6d
No known key found for this signature in database
GPG key ID: 8C76CADD40253590
5 changed files with 98 additions and 0 deletions

View file

@ -17,6 +17,13 @@ class ErrorsController < ApplicationController
def not_found = render_error 404
def show # generic page for others errors
@status = params[:status].to_i
@error_name = Rack::Utils::HTTP_STATUS_CODES[@status]
render_error @status
end
private
def render_error(status)

View file

@ -0,0 +1,19 @@
%main#content{ role: "main" }
.fr-container
.fr-my-7w.fr-mt-md-12w.fr-mb-md-10w.fr-grid-row.fr-grid-row--gutters.fr-grid-row--middle.fr-grid-row--center
.fr-py-0.fr-col-12.fr-col-md-6
%h1= @error_name
%p.fr-text--sm.fr-mb-3w Error #{@status}
%p.fr-text--lead.fr-mb-3w An error prevents this page from loading.
- if @error_name.present? # valid error code
%p.fr-text--sm.fr-mb-5w
= link_to("What does that mean?", "https://developer.mozilla.org/en/docs/Web/HTTP/Status/#{@status}", **external_link_attributes)
%ul.fr-btns-group.fr-btns-group--inline-md
%li
= link_to("Homepage", root_path, class: "fr-btn")
%li
= link_to("Contact us", contact_path, class: "fr-btn fr-btn--secondary")
= render partial: "artwork"

View file

@ -0,0 +1,19 @@
%main#content{ role: "main" }
.fr-container
.fr-my-7w.fr-mt-md-12w.fr-mb-md-10w.fr-grid-row.fr-grid-row--gutters.fr-grid-row--middle.fr-grid-row--center
.fr-py-0.fr-col-12.fr-col-md-6
%h1= @error_name
%p.fr-text--sm.fr-mb-3w Erreur #{@status}
%p.fr-text--lead.fr-mb-3w Une erreur empêche le chargement de cette page.
- if @error_name.present? # valid error code
%p.fr-text--sm.fr-mb-5w
= link_to("Quest-ce que cela veut dire ?", "https://developer.mozilla.org/fr/docs/Web/HTTP/Status/#{@status}", **external_link_attributes)
%ul.fr-btns-group.fr-btns-group--inline-md
%li
= link_to("Page daccueil", root_path, class: "fr-btn")
%li
= link_to("Contactez-nous", contact_path, class: "fr-btn fr-btn--secondary")
= render partial: "artwork"

View file

@ -700,6 +700,7 @@ Rails.application.routes.draw do
get '/404', to: 'errors#not_found'
get '/500', to: 'errors#internal_server_error'
get '/:status', to: 'errors#show', constraints: { status: /[4-5][0-5]\d/ }
if Rails.env.test?
scope 'test/api_geo' do

View file

@ -0,0 +1,52 @@
RSpec.describe ErrorsController, type: :controller do
render_views
describe 'GET #show' do
# rspec can't easily manage the exceptions_app for a real route,
# just verify the action renders correctly
let(:status_code) { 426 }
let(:status_message) { 'Upgrade Required' }
context 'HTML format' do
subject do
get :show, params: { status: status_code }, format: :html
end
it 'correctly handles and responds with an HTML response' do
subject
expect(response).to have_http_status(status_code)
expect(response.body).to include(status_message)
end
end
context 'JSON format' do
subject do
get :show, params: { status: status_code }, format: :json
end
it 'correctly handles and responds with a JSON response' do
subject
expect(response).to have_http_status(status_code)
json_response = response.parsed_body
expect(json_response['status']).to eq(status_code)
expect(json_response['name']).to eq(status_message)
end
end
end
shared_examples 'specific action' do
subject { get action_name }
it do
is_expected.to have_http_status(status_code)
end
context "404" do
let(:status_code) { 404 }
let(:action_name) { :not_found }
it_behaves_like 'specific action'
end
end
end