<commit_before>// # LastDitch
//

// [strict mode](http://mzl.la/1fRhnam)
'use strict';

var fs = require('fs');

var _ = require('lodash');
var winston = require('winston');

var Twilio = require('thehelp-messaging').Twilio;

// `constructor` has no required parameters.
function LastDitch(options) {
  _.bindAll(this);

  options = options || {};
  this.appName = options.appName || process.env.APP_NAME || 'DefaultApp';
  this.crashLog = options.crashLog || process.env.CRASH_LOG || 'logs/crash.log';

  this.development = options.development;
  if (typeof this.development === 'undefined') {
    this.development = process.env.NODE_ENV === 'development';
  }

  this.sending = false;
  this.twilio = new Twilio();

  this.setGracefulWorker(options.gracefulWorker);
}

module.exports = LastDitch;

LastDitch.prototype.setGracefulWorker = function(gracefulWorker) {
  var _this = this;

  if (gracefulWorker) {
    gracefulWorker.addCheck(function() {
      return _this.sending === false;
    });
  }
};

LastDitch.prototype.send = function(err, req, cb) {
  var stack = err ? err.stack : '';
  stack = stack.split(process.cwd()).join('');

  var entry = {
    stack: stack,
    timestamp: new Date(),
    message: 'crashed: ' + stack,
    level: 'error'
  };
  if (req && req.url) {
    entry.url = req.url;
    entry.message = 'crashed handling ' + req.url + ': ' + entry.stack;
  }

  fs.appendFileSync(this.crashLog, JSON.stringify(entry) + '\n');
  if (this.development) {
    process.nextTick(cb);
  }
  else {
    this.sendSMS(entry, cb);
  }
};

LastDitch.prototype.sendSMS = function(entry, cb) {
  var _this = this;
  var sms = {
    to: process.env.NOTIFY_SMS_TO,
    from: process.env.NOTIFY_SMS_FROM,
    body: this.appName + ' '
  };

  if (!sms.to || !sms.from) {
    if (cb) {
      process.nextTick(cb);
    }
    return;
  }

  if (entry.url) {
    sms.body += entry.url + ': ';
  }
  sms.body += entry.stack;
  sms.body = this.twilio.truncateForSMS(sms.body);

  this.sending = true;
  winston.info('Sending SMS message with crash information...');
  this.twilio.send(sms, function(err) {
    winston.info('Done sending SMS');
    _this.sending = false;
    if (cb) {
      cb(err);
    }
  });
};<commit_msg>Removing GracefulWorker and sending field<commit_after>// # LastDitch
//

// [strict mode](http://mzl.la/1fRhnam)
'use strict';

var fs = require('fs');

var _ = require('lodash');
var winston = require('winston');

var Twilio = require('thehelp-messaging').Twilio;

// `constructor` has no required parameters.
function LastDitch(options) {
  _.bindAll(this);

  options = options || {};
  this.appName = options.appName || process.env.APP_NAME || 'DefaultApp';
  this.crashLog = options.crashLog || process.env.CRASH_LOG || 'logs/crash.log';

  this.development = options.development;
  if (typeof this.development === 'undefined') {
    this.development = process.env.NODE_ENV === 'development';
  }

  this.twilio = new Twilio();
}

module.exports = LastDitch;


LastDitch.prototype.send = function(err, req, cb) {
  var stack = err ? err.stack : '';
  stack = stack.split(process.cwd()).join('');

  var entry = {
    stack: stack,
    timestamp: new Date(),
    message: 'crashed: ' + stack,
    level: 'error'
  };
  if (req && req.url) {
    entry.url = req.url;
    entry.message = 'crashed handling ' + req.url + ': ' + entry.stack;
  }

  fs.appendFileSync(this.crashLog, JSON.stringify(entry) + '\n');
  if (this.development) {
    process.nextTick(cb);
  }
  else {
    this.sendSMS(entry, cb);
  }
};

LastDitch.prototype.sendSMS = function(entry, cb) {
  var sms = {
    to: process.env.NOTIFY_SMS_TO,
    from: process.env.NOTIFY_SMS_FROM,
    body: this.appName + ' '
  };

  if (!sms.to || !sms.from) {
    if (cb) {
      process.nextTick(cb);
    }
    return;
  }

  if (entry.url) {
    sms.body += entry.url + ': ';
  }
  sms.body += entry.stack;
  sms.body = this.twilio.truncateForSMS(sms.body);

  winston.info('Sending SMS message with crash information...');
  this.twilio.send(sms, function(err) {
    winston.info('Done sending SMS');

    if (cb) {
      cb(err);
    }
  });
};
{'fraction_non_alphanumeric': 0.17028985507246377, 'fraction_numerical': 0.0004830917874396135, 'mean_word_length': 3.1618090452261307, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7215360', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1500, 'n_words': 285}