kares / request_exception_handler

Rails gem/plugin for dealing with request parameter parsing exceptions
Apache License 2.0
37 stars 8 forks source link

Rails5 support #3

Open scottbarrow opened 8 years ago

scottbarrow commented 8 years ago

When using gem "request_exception_handler"

Gem Load Error is: undefined method `parse_formatted_parameters' for class `ActionDispatch::ParamsParser'
Backtrace for gem load error is:
/usr/local/bundle/gems/request_exception_handler-0.5.0/lib/request_exception_handler.rb:111:in `alias_method'
/usr/local/bundle/gems/request_exception_handler-0.5.0/lib/request_exception_handler.rb:111:in `<class:ParamsParser>'
/usr/local/bundle/gems/request_exception_handler-0.5.0/lib/request_exception_handler.rb:109:in `<top (required)>'```

it seems `parse_formatted_parameters` disappeared around Rails 4.2.1
MichaelPoP commented 3 years ago

I think I have a fix for this...I added a block to lib/request_exception_handler.rb that takes into account the new location of the parse_formatted_parameters method in rails 5 version of actionpack. Ill see if I can submit a pull request.

`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`