Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 5172a376c434f9479e2a9861d490b4d22795742e 0 parents
@Sharagoz authored
0  exception_handler.gemspec
No changes.
BIN  lib/.exception_handler.rb.swp
Binary file not shown
20 lib/exception_handler.rb
@@ -0,0 +1,20 @@
+
+require 'exception_handler/handler.rb'
+require 'exception_handler/parser.rb'
+require 'exception_handler/show_exception.rb'
+require 'exception_handler/error_message.rb'
+require 'exception_handler/error_response_controller.rb'
+
+class ExceptionHandler
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ @app.call(env)
+ rescue Exception => e
+ Handler.new(env, e).handle_exception
+ end
+end
+
+ActionController::Dispatcher.middleware.use ExceptionHandler
BIN  lib/exception_handler/.error_message.rb.swp
Binary file not shown
BIN  lib/exception_handler/.error_response_controller.rb.swp
Binary file not shown
9 lib/exception_handler/error_message.rb
@@ -0,0 +1,9 @@
+class ErrorMessage < ActiveRecord::Base
+ establish_connection(
+ :adapter => 'mysql',
+ :host => '192.168.50.26',
+ :username => 'root',
+ :password => 'bits91HUX',
+ :database => 'bits_railshq_production'
+ )
+end
5 lib/exception_handler/error_response_controller.rb
@@ -0,0 +1,5 @@
+class ErrorResponseController < ActionController::Base
+ def index
+ render(:text => "<h1>Det oppsto en feil</h1><p>Feilmeldingen har blitt logget</p>", :layout => 'layouts/application')
+ end
+end
25 lib/exception_handler/handler.rb
@@ -0,0 +1,25 @@
+class ExceptionHandler::Handler
+ def initialize(env, exception)
+ @exception = exception
+ @env = env
+ end
+
+ def handle_exception
+ controller = @env['action_controller.instance']
+ request = ActionDispatch::Request.new(@env)
+ parsed_error = Parser.new(@exception, request, controller)
+ ErrorMessage.create(parsed_error.relevant_info) unless(parsed_error.ignore?)
+ log_error(parsed_error.relevant_info)
+ return response
+ end
+
+ def log_error(info)
+ message = "#{info[:class_name]} (#{info[:message]}):\n "
+ message += Rails.backtrace_cleaner.clean(info[:trace].split("\n")).join("\n")
+ Rails.logger.fatal(message)
+ end
+
+ def response
+ ErrorResponseController.action(:index).call(@env)
+ end
+end
43 lib/exception_handler/parser.rb
@@ -0,0 +1,43 @@
+class ExceptionHandler::Parser
+ def initialize(exception, request, controller)
+ @exception = exception
+ @request = request
+ @controller = controller
+ end
+
+ def relevant_info
+ info = {}
+ info[:class_name] = @exception.class.to_s
+ info[:message] = @exception.to_s
+ info[:trace] = @exception.backtrace.join("\n")
+ info[:target_url] = @request.url
+ info[:referer_url] = @request.referer
+ info[:params] = @request.params.inspect
+ info[:user_agent] = @request.user_agent
+ info[:user_info] = user_info
+ return info
+ end
+
+ def ignore?
+ #Ignore routing errors in requests without a referer as they are going to be bots in 99.99235% of the cases
+ routing_errors = [ActionController::RoutingError, AbstractController::ActionNotFound, ActiveRecord::RecordNotFound]
+ if(routing_errors.include?(@exception.class) && @request.referer.blank?)
+ return true
+ end
+ #Ignore requests with user_agents matching this regxp as they can be assumed to have been made by bots
+ if(@request.user_agent =~ /\b(Baidu|Gigabot|Googlebot|libwww-perl|lwp-trivial|msnbot|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg|Yandex|Jyxobot|Huaweisymantecspider|ApptusBot)\b/i)
+ return true
+ end
+ return false
+ end
+
+ def user_info
+ if(@controller.respond_to?(:current_user))
+ current_user = @controller.current_user
+ [:login, :username, :email].each do |field|
+ return current_user.send(field) if(current_user.respond_to?(field))
+ end
+ end
+ return nil
+ end
+end
10 lib/exception_handler/show_exceptions.rb
@@ -0,0 +1,10 @@
+
+module ActionDispatch
+ class ShowExceptions
+ private
+ def render_exception_with_template(env, exception)
+ ExceptionHandler::Handler.new(env, exception).handle_exception
+ end
+ alias_method_chain :render_exception, :template
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.