require 'active_support' module Softwear module ErrorCatcher extend ActiveSupport::Concern included do rescue_from Exception, StandardError, with: :error_report_form unless Rails.env.test? end protected def iterm_annotation(content) puts "=======================================================\033]1337;AddAnnotation=#{content}\a" end def error_report_form(error) time_of_error = Time.now.strftime("%x %I:%M %p") iterm_annotation("Begin #{error.class.name} (#{time_of_error})") Rails.logger.error "**** #{error.class.name}: #{error.message} ****\n\n"\ "\t#{error.backtrace.join("\n\t")}" iterm_annotation("End #{error.class.name} (#{time_of_error})") @error = error @additional_info = gather_additional_info begin respond_to do |format| format.html { render 'softwear/errors/internal_server_error', layout: layout_for_error, status: 500 } format.js { render 'softwear/errors/internal_server_error', layout: layout_for_error, status: 500 } format.json { render json: '{}', status: 500 } end rescue AbstractController::DoubleRenderError => e Rails.logger.error "DOUBLE RENDER ERROR IN CONTROLLER ERROR CATCHER!!! #{e.message}" end end def filter_params(params) new_hash = {} params.each do |key, value| new_value = value case value when Hash new_value = filter_params(value) else case key.to_s when /cc_number/ then new_value = "" when /cc_cvc/ then new_value = "" when /password/ then new_value = "" end end new_hash[key] = new_value end new_hash end def gather_additional_info JSON.pretty_generate(filter_params(params)) + "|||" + instance_variables .reject { |v| /^@_/ =~ v.to_s || %i(@view_renderer @output_buffer @view_flow @error).include?(v) } .map { |v| "#{v}: #{instance_variable_get(v).inspect rescue '(ERROR)'}" } .join("|||") end def layout_for_error user = try(:current_user) || @current_user user ? "application" : "no_overlay" end end end