<commit_before>
require 'rest-core/test'

describe RC::ErrorHandler do
  client = RC::Builder.client do
    use RC::ErrorHandler
    run RC::Dry
  end

  exp = Class.new(Exception)

  describe 'there is an exception' do
    would 'raise an error with future' do
      lambda{
      client.new.get('/', {}, RC::FAIL => [exp.new('fail')])
      }.should.raise(exp)
    end

    would 'give an error with callback' do
      client.new.get('/', {}, RC::FAIL => [exp.new('fail')]){ |res|
        res.should.kind_of?(exp)
      }
    end
  end

  describe 'error_handler gives an exception' do
    would 'raise an error with future' do
      lambda{
      client.new(:error_handler => lambda{ |res| exp.new }).
        get('/', {}, RC::FAIL => [true])
      }.should.raise(exp)
    end

    would 'give an error with callback' do
      client.new(:error_handler => lambda{ |res| exp.new }).
        get('/', {}, RC::FAIL => [true]){ |res| res.should.kind_of?(exp) }
    end
  end

  would 'no exception but errors' do
    client.new(:error_handler => lambda{ |res| 1 }).
      request(RC::FAIL => [0], RC::RESPONSE_KEY => RC::FAIL).should.eq [0, 1]
  end

  would 'set full backtrace' do
    url = 'http://example.com/'
    client = RC::Builder.client do
      use RC::ErrorHandler, lambda{ |env|
                              RuntimeError.new(env[RC::RESPONSE_BODY]) }
      use RC::ErrorDetectorHttp
    end.new
    stub_request(:get, url).to_return(:status => 404, :body => 'nnf')
    client.get(url) do |error|
      error.backtrace.grep(/^#{__FILE__}/).should.not.empty?
    end
    client.wait
  end
end<commit_msg>also test against the error message<commit_after>
require 'rest-core/test'

describe RC::ErrorHandler do
  client = RC::Builder.client do
    use RC::ErrorHandler
    run RC::Dry
  end

  exp = Class.new(Exception)

  describe 'there is an exception' do
    would 'raise an error with future' do
      lambda{
      client.new.get('/', {}, RC::FAIL => [exp.new('fail')])
      }.should.raise(exp)
    end

    would 'give an error with callback' do
      client.new.get('/', {}, RC::FAIL => [exp.new('fail')]){ |res|
        res.should.kind_of?(exp)
      }
    end
  end

  describe 'error_handler gives an exception' do
    would 'raise an error with future' do
      lambda{
      client.new(:error_handler => lambda{ |res| exp.new }).
        get('/', {}, RC::FAIL => [true])
      }.should.raise(exp)
    end

    would 'give an error with callback' do
      client.new(:error_handler => lambda{ |res| exp.new }).
        get('/', {}, RC::FAIL => [true]){ |res| res.should.kind_of?(exp) }
    end
  end

  would 'no exception but errors' do
    client.new(:error_handler => lambda{ |res| 1 }).
      request(RC::FAIL => [0], RC::RESPONSE_KEY => RC::FAIL).should.eq [0, 1]
  end

  would 'set full backtrace' do
    url = 'http://example.com/'
    client = RC::Builder.client do
      use RC::ErrorHandler, lambda{ |env|
                              RuntimeError.new(env[RC::RESPONSE_BODY]) }
      use RC::ErrorDetectorHttp
    end.new
    stub_request(:get, url).to_return(:status => 404, :body => 'nnf')
    client.get(url) do |error|
      error.message.should.eq 'nnf'
      error.backtrace.grep(/^#{__FILE__}/).should.not.empty?
    end
    client.wait
  end
end
{'fraction_non_alphanumeric': 0.16027980535279807, 'fraction_numerical': 0.004257907542579075, 'mean_word_length': 2.6262403528114664, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '6688941', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1082, 'n_words': 282}