Merge pull request #11038 from demarches-simplifiees/fix_404_as_500_in_production

Corrige les 404 qui finissaient en erreur 500 en production
This commit is contained in:
Mathieu Magnin 2024-11-14 10:19:15 +00:00 committed by GitHub
commit 19e46ac619
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 10 deletions

View file

@ -1,7 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class ErrorsController < ApplicationController class ErrorsController < ApplicationController
rescue_from Exception do rescue_from StandardError do |exception|
Sentry.capture_exception(exception)
# catch any error, except errors triggered by middlewares outside controller (like warden middleware) # 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 render file: Rails.public_path.join('500.html'), layout: false, status: :internal_server_error
end end
@ -23,15 +24,6 @@ class ErrorsController < ApplicationController
render_error @status render_error @status
end 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 # Intercept errors in before_action when fetching user or roles
# when db is unreachable so we can still display a nice 500 static page # when db is unreachable so we can still display a nice 500 static page
def current_user def current_user
@ -45,4 +37,13 @@ class ErrorsController < ApplicationController
rescue rescue
nil nil
end 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
end end

View file

@ -3,6 +3,15 @@
describe 'Errors handling', js: false do describe 'Errors handling', js: false do
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
scenario 'not found returns 404' do
without_detailed_exceptions do
visit '/nonexistent-path'
end
expect(page).to have_http_status(:not_found)
expect(page).to have_content('Page non trouvée')
end
scenario 'bug renders dynamic 500 page' do scenario 'bug renders dynamic 500 page' do
procedure.revisions.destroy_all # break procedure procedure.revisions.destroy_all # break procedure