From 6238751e4d560772bae49e1811e27400ac710dc3 Mon Sep 17 00:00:00 2001 From: FlexWage Date: Mon, 21 Jun 2021 15:35:54 -0700 Subject: [PATCH] Added fix for rails 5 issue involving parse_formatted_parameters undefined method error --- lib/request_exception_handler.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/request_exception_handler.rb b/lib/request_exception_handler.rb index bd8d420..2aa219a 100644 --- a/lib/request_exception_handler.rb +++ b/lib/request_exception_handler.rb @@ -104,7 +104,30 @@ def check_request_exception # NOTE: Rails "parameters-parser" monkey patching follows : -if defined? ActionDispatch::ParamsParser::ParseError # Rails 4.x +if defined? ActionDispatch::Http::Parameters # Rails 5.x + module ActionDispatch::Http::Parameters + alias_method 'parse_formatted_parameters_without_exception_handler', 'parse_formatted_parameters' + + def parse_formatted_parameters_with_exception_handler(env) + begin + out = parse_formatted_parameters_without_exception_handler(env) + RequestExceptionHandler.reset_request_exception # make sure it's nil + out + rescue ParseError => e + e = e.original_exception + handler = RequestExceptionHandler.parse_request_parameters_exception_handler + handler ? handler.call(ActionDispatch::Request.new(env), e) : raise + rescue => e # all Exception-s get wrapped into ParseError ... but just in case + handler = RequestExceptionHandler.parse_request_parameters_exception_handler + handler ? handler.call(ActionDispatch::Request.new(env), e) : raise + end + end + + alias_method 'parse_formatted_parameters', 'parse_formatted_parameters_with_exception_handler' + + end + +elsif defined? ActionDispatch::ParamsParser::ParseError # Rails 4.x class ActionDispatch::ParamsParser