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