Sha256: c2783d5ee8963d1c95e759a6beaf06a216b5775d4f0afd702c5faf81bb9b9dc1

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module Nadir
  class Notification
    def initialize(exception, params = {})
      @exception = exception
      @params = params
    end

    def to_params
      {
        class: @exception.class.name,
        message: @exception.message,
        location: location,
        backtrace: backtrace.join("\n"),
        environment: Nadir.config.env,
        timestamp: Time.current,
        fingerprint: fingerprint,
        host: Socket.gethostname,
        pid: Process.pid,
        request_params: @params.dig(:request, :params),
        request_remote_ip: @params.dig(:request, :remote_ip),
        request_headers: @params.dig(:request, :headers),
        user: @params.dig(:request, :user),
        job: @params.dig(:job),
      }
    end

    private

    def location
      @params[:location] || $PROGRAM_NAME
    end

    def backtrace
      @_backtrace ||=
        begin
          cleaner = ActiveSupport::BacktraceCleaner.new
          gem_paths.each { |gem_path| cleaner.add_filter { |line| line.sub(gem_path, '') } }
          cleaner.add_filter { |line| line.sub(Nadir.config.root.to_s, '') }
          cleaner.add_filter { |line| line.sub('/', '') }

          cleaner.clean(@exception.backtrace)
        end
    end

    def fingerprint
      first_backtrace_line = backtrace.find { |trace| trace !~ /pry|irb/ }
      checksum = [first_backtrace_line, @exception.class].join('|')

      Digest::SHA1.hexdigest checksum
    end

    def gem_paths
      @gem_paths ||= Gem.path | [Gem.default_dir]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nadir-1.1.2 lib/nadir/notification.rb