<commit_before>require "multi_json"

if RUBY_VERSION =~ /^1\.8/
  begin
    require "iconv"
  rescue LoadError
  end
end

require "pathname"

module Bugsnag
  class Notification
    NOTIFIER_NAME = "Ruby Bugsnag Notifier"
    NOTIFIER_VERSION = Bugsnag::VERSION
    NOTIFIER_URL = "http://www.bugsnag.com"

    API_KEY_REGEX = /[0-9a-f]{32}/i

    # e.g. "org/jruby/RubyKernel.java:1264:in `catch'"
    BACKTRACE_LINE_REGEX = /^((?:[a-zA-Z]:)?[^:]+):(\d+)(?::in `([^']+)')?$/

    # e.g. "org.jruby.Ruby.runScript(Ruby.java:807)"
    JAVA_BACKTRACE_REGEX = /^(.*)\((.*)(?::([0-9]+))?\)$/

    MAX_EXCEPTIONS_TO_UNWRAP = 5

    SUPPORTED_SEVERITIES = ["error", "warning", "info"]

    CURRENT_PAYLOAD_VERSION = "2"

    attr_accessor :context
    attr_accessor :user
    attr_accessor :configuration
    attr_accessor :meta_data

    class << self
      def deliver_exception_payload(url, payload, configuration=Bugsnag.configuration, delivery_method=nil)

        # If the payload is going to be too long, we trim the hashes to send
        # a minimal payload instead
        payload_string = Bugsnag::Helpers.dump_json(payload)
        if payload_string.length > 128000
          payload[:events].each {|e| e[:metaData] = Bugsnag::Helpers.reduce_hash_size(e[:metaData])}
          payload_string = Bugsnag::Helpers.dump_json(payload)
        end

        Bugsnag::Delivery[delivery_method || configuration.delivery_method].deliver(url, payload_string, configuration)
      end
    end

    def initialize(exception, configuration, overrides = nil, request_data = nil)
      @configuration = configuration
      @overrides = Bugsnag::Helpers.flatten_meta_data(overrides) || {}
      @request_data = request_data
      @meta_data = {}
      @user = {}
      @should_ignore = false

      self.severity = @overrides[:severity]
      @overrides.delete :severity

      if @overrides.key? :grouping_hash
        self.grouping_hash = @overrides[:grouping_hash]
        @overrides.delete :grouping_hash
      end

      if @overrides.key? :api_key
        self.api_key = @overrides[:api_key]
        @overrides.delete :api_key
      end

      if @overrides.key? :delivery_method
        @delivery_method = @overrides[:delivery_method]
        @overrides.delete :delivery_method
      end

      # Unwrap exceptions
      @exceptions = []

      ex = exception
      while ex != nil && !@exceptions.include?(ex) && @exceptions.length < MAX_EXCEPTIONS_TO_UNWRAP

        unless ex.is_a? Exception
          if ex.respond_to?(:to_exception)
            ex = ex.to_exception
          elsif ex.respond_to?(:exception)
            ex = ex.exception
          end
        end

        unless ex.is_a?(Exception) || (defined?(Java::JavaLang::Throwable) && ex.is_a?(Java::JavaLang::Throwable))
          Bugsnag.warn("Converting non-Exception to RuntimeError: #{ex.inspect}")
          ex = RuntimeError.new(ex.to_s)
        end

        @exceptions << ex

        if ex.respond_to?(:cause) && ex.cause
          ex = ex.cause<commit_msg>Get backtrace before calling middleware<commit_after>require "multi_json"

if RUBY_VERSION =~ /^1\.8/
  begin
    require "iconv"
  rescue LoadError
  end
end

require "pathname"

module Bugsnag
  class Notification
    NOTIFIER_NAME = "Ruby Bugsnag Notifier"
    NOTIFIER_VERSION = Bugsnag::VERSION
    NOTIFIER_URL = "http://www.bugsnag.com"

    API_KEY_REGEX = /[0-9a-f]{32}/i

    # e.g. "org/jruby/RubyKernel.java:1264:in `catch'"
    BACKTRACE_LINE_REGEX = /^((?:[a-zA-Z]:)?[^:]+):(\d+)(?::in `([^']+)')?$/

    # e.g. "org.jruby.Ruby.runScript(Ruby.java:807)"
    JAVA_BACKTRACE_REGEX = /^(.*)\((.*)(?::([0-9]+))?\)$/

    MAX_EXCEPTIONS_TO_UNWRAP = 5

    SUPPORTED_SEVERITIES = ["error", "warning", "info"]

    CURRENT_PAYLOAD_VERSION = "2"

    attr_accessor :context
    attr_accessor :user
    attr_accessor :configuration
    attr_accessor :meta_data

    class << self
      def deliver_exception_payload(url, payload, configuration=Bugsnag.configuration, delivery_method=nil)

        # If the payload is going to be too long, we trim the hashes to send
        # a minimal payload instead
        payload_string = Bugsnag::Helpers.dump_json(payload)
        if payload_string.length > 128000
          payload[:events].each {|e| e[:metaData] = Bugsnag::Helpers.reduce_hash_size(e[:metaData])}
          payload_string = Bugsnag::Helpers.dump_json(payload)
        end

        Bugsnag::Delivery[delivery_method || configuration.delivery_method].deliver(url, payload_string, configuration)
      end
    end

    def initialize(exception, configuration, overrides = nil, request_data = nil)
      @configuration = configuration
      @overrides = Bugsnag::Helpers.flatten_meta_data(overrides) || {}
      @request_data = request_data
      @meta_data = {}
      @user = {}
      @should_ignore = false

      self.severity = @overrides[:severity]
      @overrides.delete :severity

      if @overrides.key? :grouping_hash
        self.grouping_hash = @overrides[:grouping_hash]
        @overrides.delete :grouping_hash
      end

      if @overrides.key? :api_key
        self.api_key = @overrides[:api_key]
        @overrides.delete :api_key
      end

      if @overrides.key? :delivery_method
        @delivery_method = @overrides[:delivery_method]
        @overrides.delete :delivery_method
      end

      # Unwrap exceptions
      @exceptions = []

      ex = exception
      while ex != nil && !@exceptions.include?(ex) && @exceptions.length < MAX_EXCEPTIONS_TO_UNWRAP

        unless ex.is_a? Exception
          if ex.respond_to?(:to_exception)
            ex = ex.to_exception
          elsif ex.respond_to?(:exception)
            ex = ex.exception
          end
        end

        unless ex.is_a?(Exception) || (defined?(Java::JavaLang::Throwable) && ex.is_a?(Java::JavaLang::Throwable))
          Bugsnag.warn("Converting non-Exception to RuntimeError: #{ex.inspect}")
          ex = RuntimeError.new(ex.to_s)
          ex.set_backtrace caller
        end

        @exceptions << ex

        if ex.respond_to?(:cause) && ex.cause
          ex = ex.cause
{'fraction_non_alphanumeric': 0.12549083769633507, 'fraction_numerical': 0.007526178010471204, 'mean_word_length': 2.8543505674653216, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5871274', 'n_tokens_mistral': 2112, 'n_tokens_neox': 2015, 'n_words': 375}