app/models/exception_handler/exception.rb in exception_handler-0.7.7.0 vs app/models/exception_handler/exception.rb in exception_handler-0.8.0.0

- old
+ new

@@ -8,24 +8,17 @@ BOTS = %w(Baidu Gigabot Googlebot libwww-per lwp-trivial msnbot SiteUptime Slurp Wordpress ZIBB ZyBorg Yandex Jyxobot Huaweisymantecspider ApptusBot) # => Attributes # => Determine schema etc ATTRS = %i(class_name status message trace target referrer params user_agent) - - # => Exceptions to be rescued by ExceptionHandler - EXCEPTIONS_TO_BE_RESCUED = [ActionController::RoutingError, AbstractController::ActionNotFound].tap do |list| - list << ActiveRecord::RecordNotFound if defined?(ActiveRecord) - list << Mongoid::Errors::DocumentNotFound if defined?(Mongoid) - end ############################################################ ############################################################ # => Class (inheritance dependent on whether db option is available) - self::Exception = Class.new( - (ExceptionHandler.config.try(:db) && defined?(ActiveRecord)) ? ActiveRecord::Base : Object - ) do + self::Exception = + Class.new( (ExceptionHandler.config.try(:db) && defined?(ActiveRecord)) ? ActiveRecord::Base : Object ) do # => Include individual elements # => Only required if no db present (no ActiveRecord) if ExceptionHandler.config.try(:db) @@ -95,42 +88,42 @@ #################### # => Email # => after_initialize invoked after .new method called # => Should have been after_create but user may not save - after_initialize Proc.new { |e| ExceptionHandler::ExceptionMailer.new_exception(e).deliver } if ExceptionHandler.config.try(:email).try(:is_a?, String) + after_initialize -> (e) { ExceptionHandler::ExceptionMailer.new_exception(e).deliver }, if: :email? # => see bottom of file # => Attributes attr_accessor :request, :klass, :exception, :description attr_accessor *ATTRS unless ExceptionHandler.config.try(:db) # => Validations - validates :klass, exclusion: { in: EXCEPTIONS_TO_BE_RESCUED, message: "%{value}" }, if: -> { referer.blank? } # => might need full Proc syntax - validates :user_agent, format: { without: Regexp.new( BOTS.join("|"), Regexp::IGNORECASE ) } + validates :user_agent, format: { without: Regexp.new( BOTS.join("|"), Regexp::IGNORECASE ) } ################################## ################################## #################################### # Virtual #################################### + # => Exception (virtual) + # => Basis on which all the class is built + def exception + request.env['action_dispatch.exception'] + end + # => Klass # => Used for validation (needs to be cleaned up in 0.7.0) def klass exception.class end - # => Exception (virtual) - def exception - request.env['action_dispatch.exception'] - end - # => Description def description I18n.with_options scope: [:exception_handler], message: message, status: status do |i18n| - i18n.t response, default: Rack::Utils::HTTP_STATUS_CODES[status] || status + i18n.t response, default: Rack::Utils::HTTP_STATUS_CODES[status] end end #################################### # Exception @@ -141,11 +134,11 @@ exception.class.name end # => Message def message - exception.message + exception ? exception.message : Rack::Utils::HTTP_STATUS_CODES[status] end # => Trace def trace exception.backtrace.join("\n") @@ -179,19 +172,27 @@ # Other #################################### # => Status code (404, 500 etc) def status - ActionDispatch::ExceptionWrapper.new(request.env, exception).status_code + exception ? ActionDispatch::ExceptionWrapper.new(request.env, exception).try(:status_code) : request.env["PATH_INFO"][1..-1].to_i end # => Server Response ("Not Found" etc) def response ActionDispatch::ExceptionWrapper.rescue_responses[class_name] end ################################## ################################## + + private + + # => Email + # => should be on the same line as after_initialize but too long + def email? + ExceptionHandler.config.try(:email).try(:is_a?, String) && ExceptionHandler.config.options(status, :notification) != false + end end end ############################################################