<commit_before># Send handled exceptions to Sentry (which normally only sends unhandled exceptions).
# See https://stackoverflow.com/questions/16567243/rescue-all-errors-of-a-specific-type-inside-a-module
module ErrorHandler
  extend ActiveSupport::Concern

  included do
    include ActiveSupport::Rescuable
    rescue_from StandardError, with: :handle_standard_error
  end

  def handle_known_exceptions
    yield
  rescue => exception
    rescue_with_handler(exception)
    # reraise to run normal exception handling
    raise
  end

  def handle_standard_error(exception)
    if %w(production staging).include?(Rails.env)
      require "sentry-raven"
      Raven.capture_exception(exception)
    else
      Rails.logger.warn(exception)
    end
  end
end
<commit_msg>Add Publisher sentry context to ErrorHandler (#179)<commit_after># Send handled exceptions to Sentry (which normally only sends unhandled exceptions).
# See https://stackoverflow.com/questions/16567243/rescue-all-errors-of-a-specific-type-inside-a-module
module ErrorHandler
  extend ActiveSupport::Concern

  included do
    include ActiveSupport::Rescuable
    rescue_from StandardError, with: :handle_standard_error
  end

  def handle_known_exceptions
    yield
  rescue => exception
    rescue_with_handler(exception)
    # reraise to run normal exception handling
    raise
  end

  def handle_standard_error(exception)
    if %w(production staging).include?(Rails.env)
      require "sentry-raven"
      if publisher = introspect_publisher
        Raven.user_context(
          publisher_id: publisher.id,
          brave_publisher_id: publisher.brave_publisher_id
        )
      end
      Raven.capture_exception(exception)
    else
      Rails.logger.warn(exception)
    end
  end

  def introspect_publisher
    if defined?(@publisher) && @publisher
      return @publisher
    end
    if defined?(current_publisher) && current_publisher
      return current_publisher
    end
  end
end

{'fraction_non_alphanumeric': 0.06961382113821138, 'fraction_numerical': 0.009654471544715448, 'mean_word_length': 3.6548463356973997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '5860003', 'n_tokens_mistral': 593, 'n_tokens_neox': 572, 'n_words': 138}