Merge pull request #10235 from colinux/page-404

Pages d'erreur personnalisées / au dsfr
This commit is contained in:
Colin Darie 2024-04-15 21:12:04 +00:00 committed by GitHub
commit e3dbbf2009
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 9056 additions and 251 deletions

View file

@ -419,6 +419,17 @@ class ApplicationController < ActionController::Base
prepend_view_path "app/custom_views"
end
def try_nav_bar_profile_from_referrer
# detect context from referer, simple (no detection when refreshing the page)
params = Rails.application.routes.recognize_path(request&.referer)
controller_class = "#{params[:controller].camelize}Controller".safe_constantize
return if controller_class.nil?
controller_instance = controller_class.new
controller_instance.try(:nav_bar_profile)
end
# Extract a value from params based on the "path"
#
# params: { dossiers: { champs_public_attributes: { 1234 => { value: "hello" } } } }

View file

@ -0,0 +1,50 @@
# frozen_string_literal: true
class ErrorsController < ApplicationController
def nav_bar_profile = try_nav_bar_profile_from_referrer
rescue_from Exception do
# catch any error, except errors triggered by middlewares outside controller (like warden middleware)
render file: Rails.public_path.join('500.html'), layout: false, status: :internal_server_error
end
def internal_server_error
# This dynamic template is rendered when a "normal" error occurs, (ie. a bug which is 99.99% of errors.)
# However if this action fails (error in the view or in a middlewares)
# the exceptions are rescued and a basic 100% static html file is rendererd instead.
render_error 500
end
def not_found = render_error 404
def unprocessable_entity = render_error 422
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)
respond_to do |format|
format.html { render status: }
format.json { render status:, json: { status:, name: Rack::Utils::HTTP_STATUS_CODES[status] } }
end
end
# Intercept errors in before_action when fetching user or roles
# when db is unreachable so we can still display a nice 500 static page
def current_user
super
rescue
nil
end
def current_user_roles
super
rescue
nil
end
end

View file

@ -21,16 +21,7 @@ class ReleaseNotesController < ApplicationController
render "scrollable_list" if params[:page].present?
end
def nav_bar_profile
# detect context from referer, simple (no detection when refreshing the page)
params = Rails.application.routes.recognize_path(request&.referer)
controller_class = "#{params[:controller].camelize}Controller".safe_constantize
return if controller_class.nil?
controller_instance = controller_class.new
controller_instance.try(:nav_bar_profile)
end
def nav_bar_profile = try_nav_bar_profile_from_referrer
private