Albert Català February 2016

How to Overwrite ActionController::RoutingError on Rails application to catch "bad Ip"

I have daily bad request than generates a lot of logs in production.log like this:

F, [2016-02-08T15:39:17.698761 #2437] FATAL -- : 
ActionController::RoutingError (No route matches [GET] "/sites/default/files/elsevier/eop/S0025-7753(12)00231-X.pdf"):
  actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call...'

In my website there isn't a route for /sites/`: this is a foreign webservice error, and I want to know its IP to solve the problem.

I have learned this, but doesn't work and I think I need something else of code

(I have Rails 4.0.2 and I will upgrade to 4.2.5 soon.)

1- In routes.rb, at the end:

 match '*path', :to => 'application#routing_error', via: :all

2- In application_ontroller: I log the IP and then I need to render the 404.html if I'm on production or render the rails error normaly if I'm on development

  def routing_error(error = 'Routing error', status = :not_found, exception=nil)
    logger.debug { "---> I catch you #{request.remote_ip}" }
    render status: 404, file: "404.html"
  end

Answers


LHH February 2016

In your application_controller simply write below code -

rescue_from ActionController::RoutingError, with: lambda { |exception| render_error(404, exception) }

def render_error(status, exception)
   respond_to do |format|
     format.html { render template: "public/#{status}", layout: false, status: status }
     format.all { render nothing: true, status: status }
   end
end

You can put a condition in a method to check rails environment.

Hope this helps!!


Albert Català February 2016

Solution:

in routes.rb

class RoutingErrorConstrain
  def initialize
    @prodenv=Rails.env=="production"
  end
  def matches?(request)
    @prodenv
  end
end

MyApp::Application.routes.draw do

...
  match '*path', :to => 'application#render_routing_error', via: :all, constraints: RoutingErrorConstrain.new
...
end

in application_controller.rb:

  def render_routing_error
    logger.warn { "BAD IP:--------------#{request.ip}---remote_ip:---#{request.remote_ip}---" }
    respond_to do |format|
      format.html { render file: "public/404.html", layout: false, status: status }
      format.all { render nothing: true, status: status }
    end
  end

Post Status

Asked in February 2016
Viewed 3,232 times
Voted 10
Answered 2 times

Search




Leave an answer