static translateError()

in common/transport/http/src/rest_api_client.ts [294:367]


  static translateError(body: any, response: any): HttpTransportError {
    /*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_012: [Any error object returned by `translateError` shall inherit from the generic `Error` Javascript object and have 3 properties:
    - `response` shall contain the `IncomingMessage` object returned by the HTTP layer.
    - `responseBody` shall contain the content of the HTTP response.
    - `message` shall contain a human-readable error message.]*/
    let error: HttpTransportError;
    const errorContent = HttpBase.parseErrorBody(body);
    const message = errorContent ? errorContent.message : 'Error: ' + body;

    switch (response.statusCode) {
      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:
        /*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_021: [`translateError` shall return an `InvalidEtagError` if the HTTP response status code is `412`.]*/
        error = new errors.InvalidEtagError(message);
        break;
      case 429:
        /*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_022: [`translateError` shall return an `ThrottlingError` if the HTTP response status code is `429`.]*/
        error = new errors.ThrottlingError(message);
        break;
      case 500:
        /*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_023: [`translateError` shall return an `InternalServerError` if the HTTP response status code is `500`.]*/
        error = new errors.InternalServerError(message);
        break;
      case 502:
        /*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_024: [`translateError` shall return a `BadDeviceResponseError` if the HTTP response status code is `502`.]*/
        error = new errors.BadDeviceResponseError(message);
        break;
      case 503:
        /*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_025: [`translateError` shall return an `ServiceUnavailableError` if the HTTP response status code is `503`.]*/
        error = new errors.ServiceUnavailableError(message);
        break;
      case 504:
        /*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_026: [`translateError` shall return a `GatewayTimeoutError` if the HTTP response status code is `504`.]*/
        error = new errors.GatewayTimeoutError(message);
        break;
      default:
        /*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_013: [If the HTTP error code is unknown, `translateError` should return a generic Javascript `Error` object.]*/
        error = new Error(message);
    }

    error.response = response;
    error.responseBody = body;
    return error;
  }