<commit_before>require 'gh'

module GH
  class Error < Exception
    attr_reader :info

    def initialize(error = nil, payload = nil, info = {})
      info   = info.merge(error.info) if error.respond_to? :info and Hash === error.info
      error  = error.error while error.respond_to? :error
      @info  = info.merge(error: error, payload: payload)
      set_backtrace error.backtrace if error
    end

    def payload
      info[:payload]
    end

    def error
      info[:error]
    end

    def message
      "GH request failed\n" + info.map { |k,v| entry(k,v) }.join("\n")
    end

    private

    def entry(key, value)
      value = "#{value.class}: #{value.message}" if Exception === value
      value = value.inspect unless String === value
      value = value.gsub(/[^\n]{30}/, "\\0\n").lines.map { |l| "\n    #{l}" }.join if value.size > 30 or value.include? "\n"
      (key.to_s + ": ").ljust(12) + value
    end
  end
end
<commit_msg>wrap lines at 80, not 30<commit_after>require 'gh'

module GH
  class Error < Exception
    attr_reader :info

    def initialize(error = nil, payload = nil, info = {})
      info   = info.merge(error.info) if error.respond_to? :info and Hash === error.info
      error  = error.error while error.respond_to? :error
      @info  = info.merge(error: error, payload: payload)
      set_backtrace error.backtrace if error
    end

    def payload
      info[:payload]
    end

    def error
      info[:error]
    end

    def message
      "GH request failed\n" + info.map { |k,v| entry(k,v) }.join("\n")
    end

    private

    def entry(key, value)
      value = "#{value.class}: #{value.message}" if Exception === value
      value = value.inspect unless String === value
      value = value.gsub(/[^\n]{80}/, "\\0\n").lines.map { |l| "\n    #{l}" }.join.gsub(/\n+/, "\n")
      (key.to_s + ": ").ljust(12) + value
    end
  end
end

{'fraction_non_alphanumeric': 0.15013262599469496, 'fraction_numerical': 0.008488063660477453, 'mean_word_length': 2.429090909090909, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, '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': '456371', 'n_tokens_mistral': 650, 'n_tokens_neox': 625, 'n_words': 180}