Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function translateError(mqttError: Error): MqttTransportError {
let err: MqttTransportError;
if (mqttError.message) {
/* Codes_SRS_NODE_DEVICE_MQTT_ERRORS_18_002: [** `translateError` shall return a `NotConnectedError` if the MQTT error message contains the string 'client disconnecting' **]** */
if (mqttError.message.indexOf('client disconnecting') > -1) {
err = new errors.NotConnectedError('mqtt.js returned ' + mqttError.message + ' error');
} else if (mqttError.message.indexOf('Invalid topic') > -1) {
/* Codes_SRS_NODE_DEVICE_MQTT_ERRORS_18_003: [** `translateError` shall return a `FormatError` if the MQTT error message contains the string 'Invalid topic' **]** */
err = new errors.FormatError('mqtt.js returned ' + mqttError.message + ' error');
} else if (mqttError.message.indexOf('No connection to broker') > -1) {
/* Codes_SRS_NODE_DEVICE_MQTT_ERRORS_18_004: [** `translateError` shall return a `NotConnectedError` if the MQTT error message contains the string 'No connection to broker' **]** */
err = new errors.NotConnectedError('mqtt.js returned ' + mqttError.message + ' error');
} else if (mqttError.message.indexOf('Unacceptable protocol version') > -1) {
/* Codes_SRS_NODE_DEVICE_MQTT_ERRORS_18_005: [** `translateError` shall return a `NotImplementedError` if the MQTT error message contains the string 'Unacceptable protocol version' **]** */
err = new errors.NotImplementedError('mqtt.js returned ' + mqttError.message + ' error');
} else if (mqttError.message.indexOf('Identifier rejected') > -1) {
/* Codes_SRS_NODE_DEVICE_MQTT_ERRORS_18_006: [** `translateError` shall return a `UnauthorizedError` if the MQTT error message contains the string 'Identifier rejected' **]** */
err = new errors.UnauthorizedError('mqtt.js returned ' + mqttError.message + ' error');
} else if (mqttError.message.indexOf('Server unavailable' ) > -1) {
/* Codes_SRS_NODE_DEVICE_MQTT_ERRORS_18_007: [** `translateError` shall return a `ServiceUnavailableError` if the MQTT error message contains the string 'Server unavailable' **]** */
err = new errors.ServiceUnavailableError('mqtt.js returned ' + mqttError.message + ' error');
} else if (mqttError.message.indexOf('Bad username or password') > -1) {
var errCallback = function (error) {
var err = error || new errors.NotConnectedError('Unable to establish a connection');
self.client.removeListener('close', errCallback);
self.client.removeListener('offline', errCallback);
self.client.removeListener('disconnect', errCallback);
self.client.removeListener('error', errCallback);
done(err);
};
Amqp.prototype.send = function send(message, endpoint, to, done) {
/*Codes_SRS_NODE_COMMON_AMQP_16_006: [The send method shall construct an AMQP message using information supplied by the caller, as follows:
The ‘to’ field of the message should be set to the ‘to’ argument.
The ‘body’ of the message should be built using the message argument.] */
var amqpMessage = AmqpMessage.fromMessage(message);
amqpMessage.properties.to = to;
if (!this._connected) done(new errors.NotConnectedError('Cannot send while disconnected.'));
var sendAction = function (sender, msg, done) {
sender.send(msg)
.then(function (state) {
if (done) {
var result = new results.MessageEnqueued(state);
done(null, result);
}
})
.catch(function (err) {
/*Codes_SRS_NODE_IOTHUB_AMQPCOMMON_16_007: [If sendEvent encounters an error before it can send the request, it shall invoke the done callback function and pass the standard JavaScript Error object with a text description of the error (err.message).]*/
if (done) done(err);
});
};
if (!this._sender) {
.catch(function (err) {
/*Codes_SRS_NODE_IOTHUB_AMQPCOMMON_16_007: [If sendEvent encounters an error before it can send the request, it shall invoke the `done` callback function and pass the standard JavaScript Error object with a text description of the error (err.message).]*/
var error = new errors.NotConnectedError('AMQP: Could not create sender');
error.amqpError = err;
safeCallback(done, error);
});
}
.catch(function (err) {
var error = new errors.NotConnectedError('AMQP: Could not create receiver');
error.amqpError = err;
/*Codes_SRS_NODE_COMMON_AMQP_16_021: [The `attachReceiverLink` method shall call the `done` callback with an `Error` object if the link object wasn't created successfully.]*/
safeCallback(done, error);
});
}
Amqp.prototype.send = function send(message, endpoint, to, done) {
if (!this._connected) {
safeCallback(done, new errors.NotConnectedError('Cannot send while disconnected.'));
} else {
/*Codes_SRS_NODE_COMMON_AMQP_16_006: [The `send` method shall construct an AMQP message using information supplied by the caller, as follows:
The ‘to’ field of the message should be set to the ‘to’ argument.
The ‘body’ of the message should be built using the message argument.] */
var amqpMessage = AmqpMessage.fromMessage(message);
if (to !== undefined) {
amqpMessage.properties.to = to;
}
var sendAction = function (sender, msg, done) {
sender.send(msg)
.then(function (state) {
safeCallback(done, null, new results.MessageEnqueued(state));
return null;
})
initializeCBS: (callback) => callback(new errors.NotConnectedError()),
putToken: (audience, token, callback) => callback(new errors.NotConnectedError()),
sendMethodResponse: (response, callback) => callback(new errors.NotConnectedError('Method Links were detached - the service already considers this method failed')),
/*Codes_SRS_NODE_AMQP_DEVICE_METHOD_CLIENT_16_006: [The `onDeviceMethod` method shall save the `callback` argument so that it is called when the corresponding method call is received.]*/
disconnected: (context: EventContext) => {
let callback = this._connectionCallback;
this._connectionCallback = undefined;
manageConnectionHandlers('removeListener');
this._fsm.transition('disconnected', callback, new errors.NotConnectedError('rhea: connection disconnected'));
},
'*': () => this._fsm.deferUntilTransition()
Amqp.prototype.attachReceiverLink = function attachReceiverLink(endpoint, linkOptions, done) {
/*Codes_SRS_NODE_COMMON_AMQP_16_017: [The `attachReceiverLink` method shall throw a ReferenceError if the `endpoint` argument is falsy.]*/
if (!endpoint) {
throw new ReferenceError('endpoint cannot be \'' + endpoint + '\'');
}
/*Codes_SRS_NODE_COMMON_AMQP_16_033: [The `attachReceiverLink` method shall call the `done` callback with a `NotConnectedError` object if the amqp client is not connected when the method is called.]*/
if (!this._connected) {
safeCallback(done, new errors.NotConnectedError('Cannot send while disconnected.'));
} else {
var self = this;
var connectionError = null;
var clientErrorHandler = function(err) {
connectionError = err;
};
/*Codes_SRS_NODE_COMMON_AMQP_16_007: [If send encounters an error before it can send the request, it shall invoke the `done` callback function and pass the standard JavaScript Error object with a text description of the error (err.message).]*/
this._amqp.on('client:errorReceived', clientErrorHandler);
/*Codes_SRS_NODE_COMMON_AMQP_06_004: [The `attachReceiverLink` method shall create a policy object that contain link options to be merged if the linkOptions argument is not falsy.]*/
/*Codes_SRS_NODE_COMMON_AMQP_16_018: [The `attachReceiverLink` method shall call `createReceiver` on the `amqp10` client object.]*/
self._amqp.createReceiver(endpoint, linkOptions)
.then(function (receiver) {
self._amqp.removeListener('client:errorReceived', clientErrorHandler);
if (!connectionError) {
self._receivers[endpoint] = new AmqpReceiver(receiver);