Sha256: 180d1ec3e3970d7a63548de54056b5637b2e8ec640014e70d79b07415dec5420

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

module NdrError
  # Module to contain helpers for logging
  module Logging
    # Which attributes can be populated when manually logging an exception:
    ANCILLARY_ATTRS_WHITELIST = [:user_id, :user_roles, :svn_revision].freeze

    # Log the given `exception`.
    def log(exception, ancillary_data, request_object)
      log = initialize_log(ancillary_data)
      log.register_exception(exception)
      log.register_request(request_object)

      print = Fingerprint.find_or_create_by_id(log.md5_digest)
      error = print.store_log(log)

      [print, error]
    end

    private

    # Manual attribute whitelisting:
    def initialize_log(ancillary_data)
      Log.new.tap do |log|
        ancillary_data.symbolize_keys.each do |key, value|
          fail "Mass-assigning #{key} is forbidden!" unless ANCILLARY_ATTRS_WHITELIST.include?(key)

          if ActiveRecord::Base.respond_to?(:protected_attributes)
            log.assign_attributes({ key => value }, without_protection: true)
          else
            log.assign_attributes(key => value)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ndr_error-1.1.4 lib/ndr_error/logging.rb
ndr_error-1.1.3 lib/ndr_error/logging.rb
ndr_error-1.1.2 lib/ndr_error/logging.rb
ndr_error-1.1.1 lib/ndr_error/logging.rb
ndr_error-1.0.1 lib/ndr_error/logging.rb