<commit_before>// generic low level reusuable utils for interacting with exchanges.

const retry = require('retry');
const errors = require('./exchangeErrors');
const _ = require('lodash');

const retryInstance = (options, checkFn, callback) => {
  if(!options) {
    options = {
      retries: 100,
      factor: 1.2,
      minTimeout: 1 * 1000,
      maxTimeout: 4 * 1000
    };
  }

  let attempt = 0;

  const operation = retry.operation(options);
  operation.attempt(function(currentAttempt) {
    checkFn((err, result) => {
      if(!err) {
        return callback(undefined, result);
      }

      let maxAttempts = err.retry;
      if(maxAttempts === true)
        maxAttempts = 10;

      if(err.retry && attempt++ < maxAttempts) {
        return operation.retry(err);
      }

      if(err.notFatal) {
        if(err.backoffDelay) {
          return setTimeout(() => operation.retry(err), err.backoffDelay);
        }

        return operation.retry(err);
      }

      callback(err, result);
    });
  });<commit_msg>[GB] log all exchange erors<commit_after>// generic low level reusuable utils for interacting with exchanges.

const retry = require('retry');
const errors = require('./exchangeErrors');
const _ = require('lodash');

const retryInstance = (options, checkFn, callback, e) => {
  if(!options) {
    options = {
      retries: 100,
      factor: 1.2,
      minTimeout: 1 * 1000,
      maxTimeout: 4 * 1000
    };
  }

  let attempt = 0;

  const operation = retry.operation(options);
  operation.attempt(function(currentAttempt) {
    checkFn((err, result) => {

      if(!err) {
        return callback(undefined, result);
      }

      console.log(new Date, err.message);

      let maxAttempts = err.retry;
      if(maxAttempts === true)
        maxAttempts = 10;

      if(err.retry && attempt++ < maxAttempts) {
        return operation.retry(err);
      }

      if(err.notFatal) {
        if(err.backoffDelay) {
          return setTimeout(() => operation.retry(err), err.backoffDelay);
        }

        return operation.retry(err);
      }

      callback(err, result);
    });
  });
{'fraction_non_alphanumeric': 0.1371994342291372, 'fraction_numerical': 0.016973125884016973, 'mean_word_length': 2.507438016528926, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '207576', 'n_tokens_mistral': 693, 'n_tokens_neox': 635, 'n_words': 148}