<commit_before>'use strict';

var Thrift = require('./thrift-runtime');
var Preconditions = require('precondition');
var util = require('../common/utils');
var clone = require('clone');

var httpConfig = {transformResponse: [], transformRequest: [], timeout: 30000};

var ThriftFunctionClient = function ThriftFunctionClient(endpointUrl, serviceName, functionName, paramArray, $q, $http, ThriftRetryHandler) {
  Preconditions.checkType(util.isNotEmptyString(endpointUrl), 'Expected endpoint url, but was %s', endpointUrl);
  Preconditions.checkType(util.isNotEmptyString(serviceName), 'Expected service name, but was %s', serviceName);
  Preconditions.checkType(util.isNotEmptyString(functionName), 'Expected function name, but was %s', functionName);
  Preconditions.checkType(util.isArray(paramArray), 'Expected an array of parameter names, but was %s', paramArray);

  var clientCtorName = serviceName + 'Client';
  var ClientCtor = window[clientCtorName];
  Preconditions.checkType(util.isFunction(ClientCtor), 'No global constructor for thrift client: %s', clientCtorName);

  var self = this;
  var deferred = $q.defer();
  var rejected = false;
  var resolved = false;
  var callCount = 0;
  var retryContext = {};
  var args = null;

  function reject(reason) {
    rejected = true;
    deferred.reject(reason);
  }

  function getState() {
    var argsClone = clone(args);
    argsClone.pop();

    return {
      args: argsClone,
      serviceName: serviceName,
      functionName: functionName,
      paramArray: clone(paramArray),
      endpointUrl: endpointUrl,
      callCount: callCount
    };
  }

  function thriftCallback(result) {
    if (result instanceof Error) {
      if (util.isSomething(ThriftRetryHandler)) {
        ThriftRetryHandler.handleError(self, result, getState());
      } else {
        reject(result);
      }
    } else {
      deferred.resolve(result);
    }
  }

  function httpErrorCallback(data, status) {
    var httpFail = {data: data, status: status, type: 'httpfail'};

    if (util.isSomething(ThriftRetryHandler)) {
      ThriftRetryHandler.handleError(self, httpFail, getState());
    } else {
      reject(httpFail);
    }
  }

  this.doCall = function doCall() {
    Preconditions.checkType(rejected === false, 'Can\'t retry a thrift service call that has already completed with a reject.');
    Preconditions.checkType(resolved === false, 'Cant\'t retry a thrift service call that has already resolved.');

    if (args === null) {
      args = Array.prototype.slice.call(arguments);
    }

    for (var i = 0; i < paramArray.length; i++) {
      // we could be more strict about type checking
      Preconditions.checkType(util.isSomething(args[i]), 'Missing %s argument.', paramArray[i]);
    }

    var transport = new Thrift.Transport('');
    var protocol = new Thrift.Protocol(transport);
    var client = new ClientCtor(protocol);

    var thriftSend = client['send_' + functionName];
    var thriftRecv = client['recv_' + functionName];
    Preconditions.checkType(util.isFunction(thriftSend) && util.isFunction(thriftRecv) , 'Thrift service %s has no function %s', serviceName, functionName);

    var postData = thriftSend.apply(client, args);
    callCount++;

    try {
      var post = $http.post(endpointUrl, postData, httpConfig);
      Preconditions.checkType(!!post, 'Cant make a new http post.');
      post.then(function onSuccess(reply) {
        try {
          Preconditions.checkType(util.isObject(reply), 'Unexpected success reply: %s', reply);
          var replyJson = reply.data;
          Preconditions.checkType(util.isString(replyJson), 'Expected json reply but was %s', replyJson);
          client.output.transport.setRecvBuffer(replyJson);
          var response = thriftRecv.call(client);
          thriftCallback(response);
        } catch (e) {
          thriftCallback(e);
        }
      }, function onError(data, status) {
        console.log('Thrift rpc error.', data, status);
        if (isConnError(status)) {
          httpErrorCallback(data, status);
        } else if (status instanceof Error) {
          thriftCallback(status);
        } else {
          console.log('Unexpected thrift rpc error:', data, status);
          httpErrorCallback(data, status);
        }
      });
    } catch (e) {
      if (util.isSomething(ThriftRetryHandler)) {
        ThriftRetryHandler.handleError(self, e, getState());
      } else {<commit_msg>log more details about failed thrift calls, for easier debugging<commit_after>'use strict';

var Thrift = require('./thrift-runtime');
var Preconditions = require('precondition');
var util = require('../common/utils');
var clone = require('clone');

var httpConfig = {transformResponse: [], transformRequest: [], timeout: 30000};

var ThriftFunctionClient = function ThriftFunctionClient(endpointUrl, serviceName, functionName, paramArray, $q, $http, ThriftRetryHandler) {
  Preconditions.checkType(util.isNotEmptyString(endpointUrl), 'Expected endpoint url, but was %s', endpointUrl);
  Preconditions.checkType(util.isNotEmptyString(serviceName), 'Expected service name, but was %s', serviceName);
  Preconditions.checkType(util.isNotEmptyString(functionName), 'Expected function name, but was %s', functionName);
  Preconditions.checkType(util.isArray(paramArray), 'Expected an array of parameter names, but was %s', paramArray);

  var clientCtorName = serviceName + 'Client';
  var ClientCtor = window[clientCtorName];
  Preconditions.checkType(util.isFunction(ClientCtor), 'No global constructor for thrift client: %s', clientCtorName);

  var self = this;
  var deferred = $q.defer();
  var rejected = false;
  var resolved = false;
  var callCount = 0;
  var retryContext = {};
  var args = null;

  function reject(reason) {
    rejected = true;
    deferred.reject(reason);
  }

  function getState() {
    var argsClone = clone(args);
    argsClone.pop();

    return {
      args: argsClone,
      serviceName: serviceName,
      functionName: functionName,
      paramArray: clone(paramArray),
      endpointUrl: endpointUrl,
      callCount: callCount
    };
  }

  function thriftCallback(result, reply) {
    if (result instanceof Error) {
      console.log('Thrift error log: ', result, reply);
      if (util.isSomething(ThriftRetryHandler)) {
        ThriftRetryHandler.handleError(self, result, getState());
      } else {
        reject(result);
      }
    } else {
      deferred.resolve(result);
    }
  }

  function httpErrorCallback(data, status) {
    var httpFail = {data: data, status: status, type: 'httpfail'};

    if (util.isSomething(ThriftRetryHandler)) {
      ThriftRetryHandler.handleError(self, httpFail, getState());
    } else {
      reject(httpFail);
    }
  }

  this.doCall = function doCall() {
    Preconditions.checkType(rejected === false, 'Can\'t retry a thrift service call that has already completed with a reject.');
    Preconditions.checkType(resolved === false, 'Cant\'t retry a thrift service call that has already resolved.');

    if (args === null) {
      args = Array.prototype.slice.call(arguments);
    }

    for (var i = 0; i < paramArray.length; i++) {
      // we could be more strict about type checking
      Preconditions.checkType(util.isSomething(args[i]), 'Missing %s argument.', paramArray[i]);
    }

    var transport = new Thrift.Transport('');
    var protocol = new Thrift.Protocol(transport);
    var client = new ClientCtor(protocol);

    var thriftSend = client['send_' + functionName];
    var thriftRecv = client['recv_' + functionName];
    Preconditions.checkType(util.isFunction(thriftSend) && util.isFunction(thriftRecv) , 'Thrift service %s has no function %s', serviceName, functionName);

    var postData = thriftSend.apply(client, args);
    callCount++;

    try {
      var post = $http.post(endpointUrl, postData, httpConfig);
      Preconditions.checkType(!!post, 'Cant make a new http post.');
      post.then(function onSuccess(reply) {
        try {
          Preconditions.checkType(util.isObject(reply), 'Unexpected success reply: %s', reply);
          var replyJson = reply.data;
          Preconditions.checkType(util.isString(replyJson), 'Expected json reply but was %s', replyJson);
          client.output.transport.setRecvBuffer(replyJson);
          var response = thriftRecv.call(client);
          thriftCallback(response);
        } catch (e) {
          thriftCallback(e, reply);
        }
      }, function onError(data, status) {
        console.log('Thrift rpc error.', data, status);
        if (isConnError(status)) {
          httpErrorCallback(data, status);
        } else if (status instanceof Error) {
          thriftCallback(status);
        } else {
          console.log('Unexpected thrift rpc error:', data, status);
          httpErrorCallback(data, status);
        }
      });
    } catch (e) {
      if (util.isSomething(ThriftRetryHandler)) {
        ThriftRetryHandler.handleError(self, e, getState());
      } else {
{'fraction_non_alphanumeric': 0.12195393345944142, 'fraction_numerical': 0.0015578057193724269, 'mean_word_length': 3.482793017456359, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '3847671', 'n_tokens_mistral': 2583, 'n_tokens_neox': 2573, 'n_words': 666}