<commit_before>import http from 'http';
import urlParser from 'url';
import log from './logger';

function findError({ status, value }) {
  log(`Looking for error in status: ${status}, value: ${value}`);
  if (!status && (!value || !value.error)) {
    return null;
  }

  const { message, error } = value;


  return new Error(`WebDriverError(${error || status}): ${message}`);
}

function sendRequest(method, url, body) {
  const jsonBody = JSON.stringify(body);
  const urlParts = urlParser.parse(url);
  const options = {
    hostname: urlParts.hostname,
    port: urlParts.port,
    path: urlParts.path,
    method,
    headers: {}
  };

  if (body) {
    options.headers['Content-Length'] = Buffer.byteLength(jsonBody);
    options.headers['Content-Type'] = 'application/json';
  }


  return new Promise((resolve, reject) => {
    log(`WebDriver request: ${options.url} ${options.body}`);
    const request = http.request(options, (response) => {
      const chunks = [];
      response.setEncoding('utf8');
      response.on('data', (chunk) => {
        chunks.push(chunk);
      });
      response.on('end', () => {
        let responseBody;

        try {
          responseBody = JSON.parse(chunks.join(''));
        } catch (err) {
          reject(err);
        }

        log(`WebDriver response: ${chunks.join('')}`);

        const error = findError(responseBody);

        if (error) {
          reject(error);
          return;
        }<commit_msg>Refactor error finding in response<commit_after>import http from 'http';
import util from 'util';
import urlParser from 'url';
import log from './logger';

function findError({ status, value }) {
  const hasError = status || (value && value.error);
  log(
    `Looking for error in status: ${status}, value: ${util.inspect(
      value,
      false,
      10
    )}, hasError: ${hasError}`
  );

  if (!hasError) {
    return null;
  }

  const { message, error } = value;

  return new Error(`WebDriverError(${error || status}): ${message}`);
}

function sendRequest(method, url, body) {
  const jsonBody = JSON.stringify(body);
  const urlParts = urlParser.parse(url);
  const options = {
    hostname: urlParts.hostname,
    port: urlParts.port,
    path: urlParts.path,
    method,
    headers: {}
  };

  if (body) {
    options.headers['Content-Length'] = Buffer.byteLength(jsonBody);
    options.headers['Content-Type'] = 'application/json';
  }

  return new Promise((resolve, reject) => {
    log(`WebDriver request: ${options.url} ${options.body}`);
    const request = http.request(options, response => {
      const chunks = [];
      response.setEncoding('utf8');
      response.on('data', chunk => {
        chunks.push(chunk);
      });
      response.on('end', () => {
        let responseBody;

        try {
          responseBody = JSON.parse(chunks.join(''));
        } catch (err) {
          reject(err);
        }

        log(`WebDriver response: ${chunks.join('')}`);

        const error = findError(responseBody);

        if (error) {
          reject(error);
          return;
        }
{'fraction_non_alphanumeric': 0.1599610642439974, 'fraction_numerical': 0.0012978585334198572, 'mean_word_length': 2.7460510328068044, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '1139722', 'n_tokens_mistral': 933, 'n_tokens_neox': 920, 'n_words': 221}