Sha256: 6abff6f9ccee311e420dea42ec0db58b37f31d7dbc2335e65816f2fb8d3ba636

Contents?: true

Size: 1.63 KB

Versions: 7

Compression:

Stored size: 1.63 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 = %i[user_id user_roles svn_revision].freeze

    # Log the given `exception`.
    def log(exception, ancillary_data, request_object)
      # Capture details about a parent exception, if possible:
      parent_print, = exception.cause && log(exception.cause, ancillary_data, request_object)

      log = initialize_log(ancillary_data)
      log.register_exception(exception)
      log.register_request(request_object)
      log.register_parent(parent_print)

      print = Fingerprint.find_or_create_by_id(log.md5_digest)
      print.causal_error_fingerprint = parent_print
      log = print.store_log(log)

      NdrError.run_after_log_callbacks(exception, print, log)

      [print, log]
    end

    def monitor(ancillary_data: {}, request: nil, swallow: false)
      yield
    rescue Exception => exception # rubocop:disable Lint/RescueException
      data = log(exception, ancillary_data, request)
      swallow ? data : raise(exception)
    end

    private

    # Manual attribute whitelisting:
    def initialize_log(ancillary_data)
      Log.new.tap do |log|
        ancillary_data.symbolize_keys.each do |key, value|
          raise "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

7 entries across 7 versions & 1 rubygems

Version Path
ndr_error-2.4.1 lib/ndr_error/logging.rb
ndr_error-2.4.0 lib/ndr_error/logging.rb
ndr_error-2.3.2 lib/ndr_error/logging.rb
ndr_error-2.3.1 lib/ndr_error/logging.rb
ndr_error-2.3.0 lib/ndr_error/logging.rb
ndr_error-2.2.0 lib/ndr_error/logging.rb
ndr_error-2.1.0 lib/ndr_error/logging.rb