demarches-normaliennes/app/controllers/application_controller/error_handling.rb
Pierre de La Morinerie 09933454ff app: improve InvalidAuthenticityToken logging
- Log on all controllers
- Improve description of the controller action involved
- Ignore Safari bogus requests
2021-07-06 12:42:01 +02:00

29 lines
989 B
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module ApplicationController::ErrorHandling
extend ActiveSupport::Concern
included do
rescue_from ActionController::InvalidAuthenticityToken do
if cookies.count == 0
# When some browsers (like Safari) re-open a previously closed tab, they attempts
# to reload the page even if it is a POST request. But in that case, they dont
# sends any of the cookies.
#
# Ignore this error.
render plain: "Les cookies doivent être activés pour utiliser #{APPLICATION_NAME}.", status: 403
else
log_invalid_authenticity_token_error
raise # propagate the exception up, to render the default exception page
end
end
end
def log_invalid_authenticity_token_error
Sentry.with_scope do |temp_scope|
tags = {
action: "#{self.class.name}#{action_name}"
}
temp_scope.set_tags(tags)
Sentry.capture_message("ActionController::InvalidAuthenticityToken")
end
end
end