execption handle
This commit is contained in:
parent
70d5ffd95e
commit
d3dc942bf6
2
Gemfile
2
Gemfile
|
@ -8,6 +8,8 @@ unless RUBY_PLATFORM =~ /w32/
|
|||
end
|
||||
|
||||
gem 'seems_rateable', path: 'lib/seems_rateable'
|
||||
gem 'exception_notification', '~> 4.1.0.rc1'
|
||||
gem 'sidekiq', '~> 3.2.2'
|
||||
gem "rails", "3.2.13"
|
||||
gem "jquery-rails", "~> 2.0.2"
|
||||
gem "i18n", "~> 0.6.0"
|
||||
|
|
|
@ -0,0 +1,113 @@
|
|||
module ExceptionHandler
|
||||
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
|
||||
private
|
||||
|
||||
def rescue_errors
|
||||
rescue_from ::Exception, :with => :render_error
|
||||
rescue_from ::RuntimeError, :with => :render_error
|
||||
rescue_from ::ActiveRecord::RecordNotFound, :with => :render_record_not_found
|
||||
rescue_from ::ActionController::RoutingError, :with => :render_404
|
||||
rescue_from ::ActionController::UnknownController, :with => :render_error
|
||||
rescue_from ::AbstractController::ActionNotFound, :with => :render_error
|
||||
end
|
||||
end
|
||||
|
||||
# should be a public method
|
||||
#
|
||||
# add match '*path', to: 'application#routing_error', via: :all to routes.rb
|
||||
#
|
||||
def routing_error(exception=nil)
|
||||
# redirect_to root_path
|
||||
raise ::ActionController::RoutingError.new(params[:path])
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def exception_template_and_layout(page_code)
|
||||
template = "%s/errors/%s" % [identity, page_code]
|
||||
layout = "%s_%s" % [identity, send("#{identity}_signed_in?") ? :normal : :guest]
|
||||
|
||||
[template, layout]
|
||||
end
|
||||
|
||||
def render_404(exception=nil)
|
||||
template, layout = exception_template_and_layout(404)
|
||||
render template: template, layout: layout, status: 404, formats: [:html]
|
||||
end
|
||||
|
||||
def render_500(exception=nil)
|
||||
template, layout = exception_template_and_layout(500)
|
||||
render template: template, layout: layout, status: 500, formats: [:html]
|
||||
end
|
||||
|
||||
def render_record_not_found(exception=nil)
|
||||
# redirect_to root_path
|
||||
render_404(exception)
|
||||
debug_exception(exception)
|
||||
end
|
||||
|
||||
def render_error(exception)
|
||||
render_500
|
||||
debug_exception(exception)
|
||||
end
|
||||
|
||||
def debug_exception(exception)
|
||||
::Rails.logger.error exception.message
|
||||
exception.backtrace.each { |line| ::Rails.logger.error line }
|
||||
|
||||
# exception_notification
|
||||
::ExceptionNotifier.notify_exception(exception, env: ::Rails.env, data: notify_exception_data)
|
||||
rescue
|
||||
end
|
||||
|
||||
|
||||
|
||||
#
|
||||
# from https://github.com/le0pard/mongodb_logger/blob/25b29c094ab0cffb2de0e71112832dbdca3b7a6b/lib/mongodb_logger.rb
|
||||
#
|
||||
def notify_exception_data
|
||||
f_session = (request.respond_to?(:session) ? request.session : session)
|
||||
{
|
||||
method: request.method,
|
||||
action: action_name,
|
||||
controller: controller_name,
|
||||
path: request.path,
|
||||
url: request.url,
|
||||
params: (request.respond_to?(:filtered_parameters) ? request.filtered_parameters : params),
|
||||
session: mongo_fix_session_keys(f_session),
|
||||
ip: request.remote_ip,
|
||||
user_agent: request.user_agent,
|
||||
headers: http_request_headers
|
||||
}
|
||||
end
|
||||
|
||||
#
|
||||
# from https://github.com/le0pard/mongodb_logger/blob/25b29c094ab0cffb2de0e71112832dbdca3b7a6b/lib/mongodb_logger.rb
|
||||
#
|
||||
# session keys can be with dots. It is invalid keys for BSON
|
||||
def mongo_fix_session_keys(session = {})
|
||||
new_session = Hash.new
|
||||
session.to_hash.each do |i, j|
|
||||
new_session[i.gsub(/\./i, "|")] = j.inspect
|
||||
end unless session.empty?
|
||||
new_session
|
||||
end
|
||||
|
||||
# from http://stackoverflow.com/questions/2667634/include-params-request-information-in-rails-logger
|
||||
def http_request_headers
|
||||
keys = request.headers.env
|
||||
header_keys = keys.select{|header_name| header_name.match("^HTTP.*")}
|
||||
headers = request.headers.select{|header_name, header_value| header_keys.key(header_name)}
|
||||
headers
|
||||
rescue
|
||||
{}
|
||||
end
|
||||
|
||||
end
|
|
@ -30,6 +30,14 @@ module ApplicationHelper
|
|||
include PraiseTreadHelper
|
||||
include CoursesHelper
|
||||
|
||||
##exception handler
|
||||
include ::ExceptionHandler
|
||||
rescue_errors unless ::Rails.env.development?
|
||||
# Prevent CSRF attacks by raising an exception.
|
||||
# For APIs, you may want to use :null_session instead.
|
||||
# http://stackoverflow.com/questions/16258911/rails-4-authenticity-token
|
||||
protect_from_forgery with: :null_session #:exception
|
||||
|
||||
extend Forwardable
|
||||
def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
|
||||
|
||||
|
|
|
@ -249,3 +249,8 @@ contest_domain:
|
|||
default: contest.trustie.net
|
||||
course_domain:
|
||||
default: course.trustie.net
|
||||
exception_notification:
|
||||
recipients: ['trustieforge@gmail.com']
|
||||
|
||||
contact_notification:
|
||||
recipients: ['trustieforge@gmail.com']
|
Loading…
Reference in New Issue