<commit_before>'use strict'

const request = require('request')
const when = require('when')
const packageJson = require('../package')

const errorMessage = (statusCode, body) => {
  body = body || {}
  body.toString = function () {
    return `Unexpected status code: ${statusCode} - Details: ${JSON.stringify(body)}`
  }
  return body
}

const prepare = (callback) => {
  const deferred = when.defer()

  if (typeof (callback) === 'function') {
    return {
      callback: function (error, res, body) {
        if (error || res.statusCode >= 400) {
          callback(error || errorMessage(res.statusCode, body))
          return
        }

        callback(null, body)
      }
    }
  } else {
    return {
      promise: deferred.promise,
      callback: function (error, res, body) {
        if (error || res.statusCode >= 400) {
          deferred.reject(error || errorMessage(res.statusCode, body))
          return
        }

        deferred.resolve(body)<commit_msg>Fixed the way string errors are handled<commit_after>'use strict'

const request = require('request')
const when = require('when')
const packageJson = require('../package')

const errorMessage = (statusCode, body) => {
  body = body || {}
  if (typeof body === "string") {
    return 'Unexpected status code: ' + statusCode + ' - Details: ' + body;
  }
  body.toString = function () {
    return `Unexpected status code: ${statusCode} - Details: ${JSON.stringify(body)}`
  }
  return body
}

const prepare = (callback) => {
  const deferred = when.defer()

  if (typeof (callback) === 'function') {
    return {
      callback: function (error, res, body) {
        if (error || res.statusCode >= 400) {
          callback(error || errorMessage(res.statusCode, body))
          return
        }

        callback(null, body)
      }
    }
  } else {
    return {
      promise: deferred.promise,
      callback: function (error, res, body) {
        if (error || res.statusCode >= 400) {
          deferred.reject(error || errorMessage(res.statusCode, body))
          return
        }

        deferred.resolve(body)
{'fraction_non_alphanumeric': 0.1413562559694365, 'fraction_numerical': 0.0057306590257879654, 'mean_word_length': 2.6058519793459554, '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': '1306425', 'n_tokens_mistral': 620, 'n_tokens_neox': 598, 'n_words': 160}