<commit_before>'use strict';

const EventHubClient = require('azure-event-hubs').Client;
const Promise = require('promise');
const trackDependency = require('../appinsights/AppInsightsClient').trackDependency;

const eventHubConnectionString = process.env.PUBLISH_EVENTS_EVENTHUB_CONNECTION_STRING;
const eventHubPath = process.env.PUBLISH_EVENTS_EVENTHUB_PATH;
const eventHubPartition = process.env.PUBLISH_EVENTS_EVENTHUB_PARTITION;
const eventHubClient = EventHubClient.fromConnectionString(eventHubConnectionString, eventHubPath);

function sendMessages(messages) {
  return new Promise((resolve, reject) => {
    if (!messages || !messages.length) {
      return reject('No messages to be sent');
    }

    let payloads;
    try {
      payloads = messages.map(message => ({contents: JSON.stringify(message)}));
    } catch (err) {
      return reject(`Unable to create payloads for EventHub: ${err}`);
    }

    eventHubClient.open()
    .then(() => eventHubClient.createSender())
    .then(eventHubSender => {
      eventHubSender.on('errorReceived', err => reject(`Error talking to EventHub: ${err}`));
      Promise.all(payloads.map(payload => eventHubSender.send(payload, eventHubPartition)))
      .then(() => resolve([]))
      .catch((err) => reject(`Error sending EventHub message: ${err}`));
    });
  });
}

module.exports = {
  sendMessages: trackDependency(sendMessages, 'EventHub', 'send')
};<commit_msg>Reject promise if eventhub does not have connection string.<commit_after>'use strict';

const EventHubClient = require('azure-event-hubs').Client;
const Promise = require('promise');
const trackDependency = require('../appinsights/AppInsightsClient').trackDependency;

const eventHubConnectionString = process.env.PUBLISH_EVENTS_EVENTHUB_CONNECTION_STRING;
const eventHubPath = process.env.PUBLISH_EVENTS_EVENTHUB_PATH;
const eventHubPartition = process.env.PUBLISH_EVENTS_EVENTHUB_PARTITION;

function sendMessages(messages) {
  return new Promise((resolve, reject) => {
    if (!messages || !messages.length) {
      return reject('No messages to be sent');
    }

    let payloads;
    try {
      payloads = messages.map(message => ({contents: JSON.stringify(message)}));
    } catch (err) {
      return reject(`Unable to create payloads for EventHub: ${err}`);
    }

    const eventHubClient = EventHubClient.fromConnectionString(eventHubConnectionString, eventHubPath);

    if (!eventHubClient) return reject('No event hub connection string provided.');

    eventHubClient.open()
    .then(() => eventHubClient.createSender())
    .then(eventHubSender => {
      eventHubSender.on('errorReceived', err => reject(`Error talking to EventHub: ${err}`));
      Promise.all(payloads.map(payload => eventHubSender.send(payload, eventHubPartition)))
      .then(() => resolve([]))
      .catch((err) => reject(`Error sending EventHub message: ${err}`));
    });
  });
}

module.exports = {
  sendMessages: trackDependency(sendMessages, 'EventHub', 'send')
};
{'fraction_non_alphanumeric': 0.13730743469524448, 'fraction_numerical': 0.0, 'mean_word_length': 4.926587301587301, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '7376654', 'n_tokens_mistral': 858, 'n_tokens_neox': 846, 'n_words': 183}