var Promise = require('bluebird');
var EventEmitter = require('events');
var WebSocket = require('ws');
var util = require('util');


var serviceLocator = require('./service-locator');


/**
 * @param {String} identifier
 * @param {WebSocket} webSocket
 * @constructor
 */
function Connection(identifier, webSocket) {
  this.identifier = identifier;

  this.webSocket = webSocket;
  this.webSocket.on('message', function(data) {
    var message = JSON.parse(data);
    serviceLocator.get('logger').debug('<- ' + this.identifier, message);
    this._onMessage(message);
  }.bind(this));

  this.webSocket.on('error', function(error) {
    this.emit('error', error);
  }.bind(this));

  this.webSocket.on('close', function() {
    this.emit('close');
  }.bind(this));
}

util.inherits(Connection, EventEmitter);

Connection.prototype.close = function() {
  this.webSocket.terminate();
};

/**
 * @param {Object} message
 * @returns {Promise}
 */
Connection.prototype.send = function(message) {
  if (this.isOpened()) {
    return this._send(message);
  } else {
    return this._queue(message);
  }
};

/**
 * @returns {Boolean}
 */
Connection.prototype.isOpened = function() {
  return WebSocket.OPEN === this.webSocket.readyState;
};

/**
 * @param {Object} message
 * @returns {Promise}
 * @private
 */
Connection.prototype._queue = function(message) {
  var self = this;
  return new Promise(function(resolve, reject) {
    self.webSocket.once('open', function() {
      self._send(message).then(function(response) {
        resolve(response);
      });
    });
  });
};

/**
 * @param {Object} message
 * @returns {Promise}
 * @private
 */
Connection.prototype._send = function(message) {
  serviceLocator.get('logger').debug('-> ' + this.identifier, message);
  return new Promise(function(resolve, reject) {
    this.webSocket.send(JSON.stringify(message), {}, function(err) {
      if (err) {
        reject(err);
      }
      resolve();
    });
  }.bind(this));
};

/**
 * @param {Object} message
 * @private
 */
Connection.prototype._onMessage = function(message) {
  try {
    this.emit('message', message);
  } catch (e) {
    serviceLocator.get('logger').error(e, e.stack);
  }
};

module.exports = Connection;

@@ -126,17 +126,16 @@
 til');%0A%0A
-%0A
 var serv
@@ -2122,24 +2122,28 @@
 catch (e
+rror
 ) %7B%0A    
 serviceL
@@ -2138,53 +2138,32 @@
     
-serviceLocator.get('logger').
+this.emit('
 error
-(e, e.stack
+', error
 );%0A 

Remove additional mechanism for handling errors; pass error up instead