Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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) {
/* Codes_SRS_NODE_DEVICE_MQTT_ERRORS_18_008: [** `translateError` shall return a `UnauthorizedError` if the MQTT error message contains the string 'Bad username or password' **]** */
err = new errors.UnauthorizedError('mqtt.js returned ' + mqttError.message + ' error');
} else if (mqttError.message.indexOf('Not authorized') > -1) {
/* Codes_SRS_NODE_DEVICE_MQTT_ERRORS_18_009: [** `translateError` shall return a `UnauthorizedError` if the MQTT error message contains the string 'Not authorized' **]** */
err = new errors.UnauthorizedError('mqtt.js returned ' + mqttError.message + ' error');
} else if (mqttError.message.indexOf('unrecognized packet type') > -1) {
/* Codes_SRS_NODE_DEVICE_MQTT_ERRORS_18_010: [** `translateError` shall return a `InternalServerError` if the MQTT error message contains the string 'unrecognized packet type' **]** */
err = new errors.InternalServerError('mqtt.js returned ' + mqttError.message + ' error');
} else {
/* Codes_SRS_NODE_DEVICE_MQTT_ERRORS_18_011: [** `translateError` shall return an `Error` if none of the other string rules match **]** */
err = mqttError;
}
this._receivers[_putTokenReceivingEndpoint].on('message', function (msg) {
for (var i = 0; i < this._putToken.outstandingPutTokens.length; i++) {
if (msg.correlationId === this._putToken.outstandingPutTokens[i].correlationId) {
var completedPutToken = this._putToken.outstandingPutTokens[i];
this._putToken.outstandingPutTokens.splice(i, 1);
if (completedPutToken.putTokenCallback) {
/*Codes_SRS_NODE_COMMON_AMQP_06_013: [A put token response of 200 will invoke `putTokenCallback` with null parameters.]*/
var error = null;
if (msg.properties.getValue('status-code') !== 200) {
/*Codes_SRS_NODE_COMMON_AMQP_06_014: [A put token response not equal to 200 will invoke `putTokenCallback` with an error object of UnauthorizedError.]*/
error = new errors.UnauthorizedError(msg.properties.getValue('status-description'));
}
safeCallback(completedPutToken.putTokenCallback, error);
}
break;
}
}
//
// Regardless of whether we found the put token in the list of outstanding
// operations, accept it. This could be a put token that we previously
// timed out. Be happy. It made it home, just too late to be useful.
//
/*Codes_SRS_NODE_COMMON_AMQP_06_012: [All responses shall be completed.]*/
this._receivers[_putTokenReceivingEndpoint].complete(msg);
}.bind(this));
/*Codes_SRS_NODE_COMMON_AMQP_06_020: [If given as an argument, the `initializeCBS` method shall call `initializeCBSCallback` with a null error object if successful.]*/
//
if (msg.correlation_id === this._putToken.outstandingPutTokens[i].correlationId) {
const completedPutToken = this._putToken.outstandingPutTokens[i];
this._putToken.outstandingPutTokens.splice(i, 1);
//
// If this was the last outstanding put token then get rid of the timer trying to clear out expiring put tokens.
//
if (this._putToken.outstandingPutTokens.length === 0) {
clearTimeout(this._putToken.timeoutTimer);
}
if (completedPutToken.putTokenCallback) {
/*Codes_SRS_NODE_AMQP_CBS_16_019: [A put token response of 200 will invoke `putTokenCallback` with null parameters.]*/
let error = null;
if (msg.application_properties['status-code'] !== 200) {
/*Codes_SRS_NODE_AMQP_CBS_16_018: [A put token response not equal to 200 will invoke `putTokenCallback` with an error object of UnauthorizedError.]*/
error = new errors.UnauthorizedError(msg.application_properties['status-description']);
}
completedPutToken.putTokenCallback(error);
}
break;
}
}
});
this._fsm.transition('attached', callback);
export function translateError(response: any, status: number): TwinBaseError {
let error: TwinBaseError;
switch (status) {
case 400:
/*Codes_SRS_NODE_DEVICE_TWIN_ERRORS_18_003: [`translateError` shall return an `ArgumentError` if the response status code is `400`.]*/
error = new errors.ArgumentError();
break;
case 401:
/*Codes_SRS_NODE_DEVICE_TWIN_ERRORS_18_004: [`translateError` shall return an `UnauthorizedError` if the response status code is `401`.]*/
error = new errors.UnauthorizedError();
break;
case 403:
/*Codes_SRS_NODE_DEVICE_TWIN_ERRORS_18_005: [`translateError` shall return an `IotHubQuotaExceededError` if the response status code is `403`.]*/
error = new errors.IotHubQuotaExceededError();
break;
case 404:
/*Codes_SRS_NODE_DEVICE_TWIN_ERRORS_18_006: [`translateError` shall return an `DeviceNotFoundError` if the response status code is `404`.]*/
error = new errors.DeviceNotFoundError();
break;
case 413:
/*Codes_SRS_NODE_DEVICE_TWIN_ERRORS_18_007: [`translateError` shall return an `MessageTooLargeError` if the response status code is `413`.]*/
error = new errors.MessageTooLargeError();
break;
case 500:
/*Codes_SRS_NODE_DEVICE_TWIN_ERRORS_18_008: [`translateError` shall return an `InternalServerError` if the response status code is `500`.]*/
error = new errors.InternalServerError();
var translateError = function translateError(message, amqpError) {
var error;
if (amqpError.constructor.name === 'AMQPError') {
switch (amqpError.condition.contents) {
case 'amqp:not-found':
/*Codes_SRS_NODE_DEVICE_AMQP_COMMON_ERRORS_16_006: [`translateError` shall return an `DeviceNotFoundError` if the AMQP error condition is `amqp:not-found`.]*/
error = new errors.DeviceNotFoundError(message);
break;
case 'amqp:unauthorized-access':
/*Codes_SRS_NODE_DEVICE_AMQP_COMMON_ERRORS_16_004: [`translateError` shall return an `UnauthorizedError` if the AMQP error condition is `amqp:unauthorized-access`.]*/
error = new errors.UnauthorizedError(message);
break;
case 'amqp:internal-error':
/*Codes_SRS_NODE_DEVICE_AMQP_COMMON_ERRORS_16_008: [`translateError` shall return an `InternalServerError` if the AMQP error condition is `amqp:internal-error`.]*/
error = new errors.InternalServerError(message);
break;
case 'com.microsoft:timeout':
/*Codes_SRS_NODE_DEVICE_AMQP_COMMON_ERRORS_16_009: [`translateError` shall return an `ServiceUnavailableError` if the AMQP error condition is `com.microsoft:timeout`.]*/
error = new errors.ServiceUnavailableError(message);
break;
case 'amqp:link-message-size-exceeded':
/*Codes_SRS_NODE_DEVICE_AMQP_COMMON_ERRORS_16_007: [`translateError` shall return an `MessageTooLargeError` if the AMQP error condition is `amqp:link-message-size-exceeded`.]*/
error = new errors.MessageTooLargeError(message);
break;
case 'com.microsoft:argument-out-of-range':
/*Codes_SRS_NODE_DEVICE_AMQP_COMMON_ERRORS_16_003: [`translateError` shall return an `ArgumentError` if the AMQP error condition is `com.microsoft:argument-out-of-range`.]*/
error = new errors.ArgumentError(message);
function translateError(err) {
var error = err;
if (err.constructor.name === 'AMQPError') {
if (err.condition.contents === 'amqp:resource-limit-exceeded') {
error = new errors.DeviceMaximumQueueDepthExceededError(err.description);
}
else if (err.condition.contents === 'amqp:not-found') {
error = new errors.DeviceNotFoundError(err.description);
}
else if (err.condition.contents === 'amqp:unauthorized-access') {
error = new errors.UnauthorizedError(err.description);
}
else {
error = new Error(err.description);
}
error.transport = err;
}
else if (err instanceof amqp10.Errors.AuthenticationError) {
error = new errors.UnauthorizedError(err.message);
}
return error;
}
export function translateError(message: string, body: any, response: IncomingMessage): HttpTransportError {
let error: HttpTransportError;
switch (response.statusCode) {
case 400:
/*Codes_SRS_NODE_DEVICE_HTTP_ERRORS_16_003: [`translateError` shall return an `ArgumentError` if the HTTP response status code is `400`.]*/
error = new errors.ArgumentError(message);
break;
case 401:
/*Codes_SRS_NODE_DEVICE_HTTP_ERRORS_16_004: [`translateError` shall return an `UnauthorizedError` if the HTTP response status code is `401`.]*/
error = new errors.UnauthorizedError(message);
break;
case 403:
/*Codes_SRS_NODE_DEVICE_HTTP_ERRORS_16_005: [`translateError` shall return an `IotHubQuotaExceededError` if the HTTP response status code is `403`.]*/
error = new errors.IotHubQuotaExceededError(message);
break;
case 404:
/*Codes_SRS_NODE_DEVICE_HTTP_ERRORS_16_006: [`translateError` shall return an `DeviceNotFoundError` if the HTTP response status code is `404`.]*/
error = new errors.DeviceNotFoundError(message);
break;
case 413:
/*Codes_SRS_NODE_DEVICE_HTTP_ERRORS_16_007: [`translateError` shall return an `MessageTooLargeError` if the HTTP response status code is `413`.]*/
error = new errors.MessageTooLargeError(message);
break;
case 500:
/*Codes_SRS_NODE_DEVICE_HTTP_ERRORS_16_008: [`translateError` shall return an `InternalServerError` if the HTTP response status code is `500`.]*/
error = new errors.InternalServerError(message);
break;
case 'com.microsoft:precondition-failed':
error = new errors.PreconditionFailedError(message);
break;
case 'com.microsoft:quota-exceeded':
error = new errors.IotHubQuotaExceededError(message);
break;
case 'com.microsoft:timeout':
error = new errors.ServiceUnavailableError(message);
break;
default:
/*Codes_SRS_NODE_DEVICE_AMQP_COMMON_ERRORS_16_002: [If the AMQP error code is unknown, `translateError` should return a generic Javascript `Error` object.]*/
error = new Error(message);
}
} else if (amqpError instanceof Amqp10Errors.AuthenticationError) {
error = new errors.UnauthorizedError(message);
} else {
/*Codes_SRS_NODE_DEVICE_AMQP_COMMON_ERRORS_16_002: [If the AMQP error code is unknown, `translateError` should return a generic Javascript `Error` object.]*/
error = new Error(message);
}
/*Codes_SRS_NODE_DEVICE_AMQP_COMMON_ERRORS_16_001: [Any error object returned by `translateError` shall inherit from the generic `Error` Javascript object and have 2 properties:
*- `amqpError` shall contain the error object returned by the AMQP layer.
*- `message` shall contain a human-readable error message]
*/
error.amqpError = amqpError;
return error;
}
export function translateError(message: string, status: number, result?: any, response?: any): ProvisioningError {
let error: ProvisioningError;
switch (status) {
case 400:
/*Codes_SRS_NODE_DPS_ERRORS_18_002: [`translateError` shall return an `ArgumentError` if the status code is `400`.]*/
error = new errors.ArgumentError(message);
break;
case 401:
/*Codes_SRS_NODE_DPS_ERRORS_18_003: [`translateError` shall return an `UnauthorizedError` if the status code is `401`.]*/
error = new errors.UnauthorizedError(message);
break;
case 404:
/*Codes_SRS_NODE_DPS_ERRORS_18_004: [`translateError` shall return an `DeviceNotFoundError` if the status code is `404`.]*/
error = new errors.DeviceNotFoundError(message);
break;
case 429:
/*Codes_SRS_NODE_DPS_ERRORS_18_005: [`translateError` shall return an `IotHubQuotaExceededError` if the status code is `429`.]*/
error = new errors.IotHubQuotaExceededError(message);
break;
case 500:
/*Codes_SRS_NODE_DPS_ERRORS_18_006: [`translateError` shall return an `InternalServerError` if the status code is `500`.]*/
error = new errors.InternalServerError(message);
break;
default:
/*Codes_SRS_NODE_DPS_ERRORS_18_007: [If the status code is unknown, `translateError` should return a generic Javascript `Error` object.]*/
error = new Error(message);
if (err.condition.contents === 'amqp:resource-limit-exceeded') {
error = new errors.DeviceMaximumQueueDepthExceededError(err.description);
}
else if (err.condition.contents === 'amqp:not-found') {
error = new errors.DeviceNotFoundError(err.description);
}
else if (err.condition.contents === 'amqp:unauthorized-access') {
error = new errors.UnauthorizedError(err.description);
}
else {
error = new Error(err.description);
}
error.transport = err;
}
else if (err instanceof amqp10.Errors.AuthenticationError) {
error = new errors.UnauthorizedError(err.message);
}
return error;
}