Sha256: 92dd82a413af0665fae03778a9935e76fca165c00e850138884df8790928ac42

Contents?: true

Size: 1.36 KB

Versions: 13

Compression:

Stored size: 1.36 KB

Contents

# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

require 'contrast/components/logger'

module Contrast
  module Agent
    module Reporting
      # A class supporting all to_controlled_hash reporting methods. All errors raised during
      # validation will be handled here.
      class ReportableHash
        include Contrast::Components::Logger::InstanceMethods

        # Convert the instance variables on the class, and other information, into the identifiers required for
        # TeamServer to process the JSON form of this message.
        #
        # @return [Hash, nil]
        # @raise [ArgumentError]
        def to_controlled_hash
          validate
          {}
        end

        # @raise [ArgumentError]
        def validate; end

        # Helper method to get json representation of event objects and handle errors.
        #
        #  @return [String, nil] - JSON
        def event_json
          hsh = to_controlled_hash
          hsh.to_json
        rescue ArgumentError => e
          # Log the error and raise it for the client to handle:
          logger.error(validation_error_message, e)
          raise(e)
        end

        private

        def validation_error_message
          "#{ cs__class } failed validation with: "
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
contrast-agent-7.3.2 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-7.3.1 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-7.3.0 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-7.2.0 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-7.1.0 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-7.0.0 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-6.15.3 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-6.15.2 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-6.15.1 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-6.15.0 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-6.14.0 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-6.13.0 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb
contrast-agent-6.12.0 lib/contrast/agent/reporting/reporting_events/reportable_hash.rb