-
Notifications
You must be signed in to change notification settings - Fork 864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rails: ignore InvalidPage exception in production #271
Comments
I've been getting the same error periodically for the last couple weeks. ArgumentError: invalid value for Integer(): "vvxvpgwxofhmvf" (and: "jqhscznljnhlxr", "rixhuvnazokxf", "rksoidrehnh", "lbnbyiqvta")
|
best practice would be to check if your params id is a number and greater than 0, I also changed this in my fork and made a pull-reques params[:page] = 'foobar'
posts = Post.where(:published => true).page(params[:page])
#posts.current_page = 1
# set default_page if necessary
posts = Post.where(:published => true).page(params[:page], :default_page => 10)
#posts.current_page = 10
# this raises no exception, it trys to return the default_page have a look at #276 |
The exception is deliberate. However, in production will_paginate configures Rails to swallow these exceptions and return 404s. You shouldn't be seening those in the exception tracker. If you see these exceptions in production, tell me your Rails version and output of |
@mislav I'm seeing this exception in production also. Exception: invalid value for Integer(): "6/" |
OK, my bad. will_paginate just configures Rails to display a 404 for that exception in production, but the exception is still thrown internally, and exception trackers will log it. I'll have to rethink this for future versions |
Hi there, I got this issue in production on website with a lot of traffic (always get strange wrong queries). I think a quick & clean solution can be to add a rails route constraint to avoid hitting your controller with wrong values (and yeah otherwise it should raise, but maybe a custom exception would be nicer to catch it & render a 404. Oh, seems like it now the case, nvm.). Anyway, here it goes for my solution on the routes level : # lib/will_paginate_constraint.rb
class WillPaginateConstraint
def matches?(request)
request.params[:page].nil? || /^\d+$/ =~ request.params[:page]
end
end # config/routes.rb
match '/', :action => :index, :controller => :publications,
:constraints => WillPaginateConstraint.new |
Any update on this? It's been a todo for a year and a half. I'm still seeing this logged in our production exception logger. I'd ignore it in my exception tracker but it's an exception coming from ruby and not will paginate. |
What if I want to handle the InvalidPage exception manually in my ApplicationController? As I can assume now, it is handled by middleware and outputs the 404 static page, without any possibility to override this behavior (without monkeypatching of gem`s source). |
After duplicating this in #626 and thinking about a PR I am wondering how have you approached it: Is rails constraints the preferred approach to filter the params or adding logic to ApplicationController or should it be the will_paginate that understands about pagination? |
It's an interesting problem; one I always felt is better solved in the app itself.
If someone has ideas/experience addressing this, please share! |
Other params are checked in the model. ActionModel allows the validation of params in the model. The controllers only check that they exist. But will_paginate does half the job - only validates stricktly, but does not allow the configuration of the validation the same way ActiveModel allows. |
In Rails applications I see often (simplified) code like this: class UsersController < ApplicationController
def index
@users = User.paginate(page: params[:page])
end
end Since the IMHO raising any kind of error in a scope will not improve the quality of code compared to [1] and [2]: class UsersController < ApplicationController
def index
@users = User.paginate(page: params[:page]).order(:created_at)
rescue ArgumentError
@users = User.order(:created_at)
end
end And there are more params that might be used directly in the controller e.g. I will drop some thoughts here, how this could be addressed:
I'm happy to see if we can find an option @mislav likes to add to will_paginate. [1] module WillPaginateActiveRecordPaginationExtension
def paginate(options)
options[:page] = Integer(options[:page], exception: false)
super
end
end
WillPaginate::ActiveRecord::Pagination.prepend(WillPaginateActiveRecordPaginationExtension) [2] class ApplicationController < ActionController::Base
private
def sanitized_page
Integer(params[:page], exception: false)
end
end
class UsersController < ApplicationController
def index
@users = User.paginate(page: sanitized_page)
end
end |
in our logs we found this problem
ArgumentError: invalid value for Integer(): 'xoforvfmy' in "[GEM_ROOT]/gems/will_paginate-3.0.2/lib/will_paginate/page_number.rb" on line 16.
Some web crawler requested
page=xoforvfmy
I tried to find best place to fix this, but failed.
relavent backtrace:
The text was updated successfully, but these errors were encountered: