From 1e7b5a56e4b71b53048e7927869f6367e3446ae7 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 12 Nov 2024 13:26:41 +0100 Subject: [PATCH 1/2] Fix the hidden error: 'private method 'current_user' called for an instance of ErrorsController' --- app/controllers/errors_controller.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb index e44b5ee8b..dcdd1a7a5 100644 --- a/app/controllers/errors_controller.rb +++ b/app/controllers/errors_controller.rb @@ -23,15 +23,6 @@ class ErrorsController < ApplicationController 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 @@ -45,4 +36,13 @@ class ErrorsController < ApplicationController rescue nil 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 From 7fbe5dda9840dffcd2392872b0fc3ae216eba8df Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 12 Nov 2024 13:32:49 +0100 Subject: [PATCH 2/2] Capture exception on sentry if something is wrong --- app/controllers/errors_controller.rb | 3 ++- spec/system/errors_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb index dcdd1a7a5..79a394099 100644 --- a/app/controllers/errors_controller.rb +++ b/app/controllers/errors_controller.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true 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) render file: Rails.public_path.join('500.html'), layout: false, status: :internal_server_error end diff --git a/spec/system/errors_spec.rb b/spec/system/errors_spec.rb index 553ab8e14..687291b61 100644 --- a/spec/system/errors_spec.rb +++ b/spec/system/errors_spec.rb @@ -3,6 +3,15 @@ describe 'Errors handling', js: false do 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 procedure.revisions.destroy_all # break procedure