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(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);
break;
}
/* Codes_SRS_NODE_DPS_ERRORS_18_008: [Any error object returned by `translateError` shall inherit from the generic `Error` Javascript object and have 3 properties:
SimulatedAmqp.prototype.send = function send(deviceId, message, done) {
if (done) {
if (deviceId.search(/^no-device/) !== -1) {
done(new errors.DeviceNotFoundError());
}
else {
done(null, new results.MessageEnqueued());
if (message.ack === 'full') {
this._receiver.emit('message', {
body: [{
originalMessageId: message.messageId,
deviceId: deviceId
}]
});
}
}
}
};
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();
break;
case 503:
/*Codes_SRS_NODE_DEVICE_TWIN_ERRORS_18_009: [`translateError` shall return an `ServiceUnavailableError` if the response status code is `503`.]*/
error = new errors.ServiceUnavailableError();
break;
case 504:
/*Codes_SRS_NODE_DEVICE_TWIN_ERRORS_18_011: [`translateError` shall return an `ServiceUnavailableError` if the response status code is `504`.]*/
error = new errors.ServiceUnavailableError();
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;
}
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 503:
/*Codes_SRS_NODE_DEVICE_HTTP_ERRORS_16_009: [`translateError` shall return an `ServiceUnavailableError` if the HTTP response status code is `503`.]*/
error = new errors.ServiceUnavailableError(message);
break;
default:
/*Codes_SRS_NODE_DEVICE_HTTP_ERRORS_16_002: [If the HTTP error code is unknown, `translateError` should return a generic Javascript `Error` object.]*/
error = new Error(message);
| "com.microsoft:message-lock-lost" | DeviceMessageLockLostError |
| "com.microsoft:precondition-failed" | PreconditionFailedError |
| "com.microsoft:quota-exceeded" | IotHubQuotaExceededError |
| "com.microsoft:timeout" | ServiceUnavailableError |
]*/
if ((amqpError as Amqp10Errors.ProtocolError).condition) {
switch ((amqpError as Amqp10Errors.ProtocolError).condition) {
case 'amqp:internal-error':
error = new errors.InternalServerError(message);
break;
case 'amqp:link:message-size-exceeded':
error = new errors.MessageTooLargeError(message);
break;
case 'amqp:not-found':
error = new errors.DeviceNotFoundError(message);
break;
case 'amqp:not-implemented':
error = new errors.NotImplementedError(message);
break;
case 'amqp:not-allowed':
error = new errors.InvalidOperationError(message);
break;
case 'amqp:resource-limit-exceeded':
error = new errors.IotHubQuotaExceededError(message);
break;
case 'amqp:unauthorized-access':
error = new errors.UnauthorizedError(message);
break;
case 'com.microsoft:argument-error':
error = new errors.ArgumentError(message);
break;
case 400:
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_014: [`translateError` shall return an `ArgumentError` if the HTTP response status code is `400`.]*/
error = new errors.ArgumentError(message);
break;
case 401:
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_015: [`translateError` shall return an `UnauthorizedError` if the HTTP response status code is `401`.]*/
error = new errors.UnauthorizedError(message);
break;
case 403:
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_016: [`translateError` shall return an `TooManyDevicesError` if the HTTP response status code is `403`.]*/
error = new errors.TooManyDevicesError(message);
break;
case 404:
if (errorContent && errorContent.code === 'DeviceNotFound') {
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_018: [`translateError` shall return an `DeviceNotFoundError` if the HTTP response status code is `404` and if the error code within the body of the error response is `DeviceNotFound`.]*/
error = new errors.DeviceNotFoundError(message);
} else if (errorContent && errorContent.code === 'IotHubNotFound') {
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_017: [`translateError` shall return an `IotHubNotFoundError` if the HTTP response status code is `404` and if the error code within the body of the error response is `IotHubNotFound`.]*/
error = new errors.IotHubNotFoundError(message);
} else {
error = new Error('Not found');
}
break;
case 408:
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_019: [`translateError` shall return a `DeviceTimeoutError` if the HTTP response status code is `408`.]*/
error = new errors.DeviceTimeoutError(message);
break;
case 409:
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_020: [`translateError` shall return an `DeviceAlreadyExistsError` if the HTTP response status code is `409`.]*/
error = new errors.DeviceAlreadyExistsError(message);
break;
case 412:
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);