static fromAzureStorageCallbackArgs()

in device/core/src/blob_upload/blob_upload_result.ts [45:78]


  static fromAzureStorageCallbackArgs(err?: Error & RestErrorStub, uploadResponse?: BlobUploadCommonResponseStub): BlobUploadResult {
    // According to the Azure Storage JK SDK, the Azure Storage REST API returns 500/404 status Code in HTTP responses when an error occurs.
    // @azure/ms-rest-js deserialization will throw `RestError` directly, and the Storage SDK propagates this error to the top.
    // But this seems to be incorrect temporarily (Github Issue filed on part of Azure Storage SDK for JS Team).
    // Currently, the thrown `RestError` should be further deserialized according to the response headers and bodies.
    /*Codes_SRS_NODE_DEVICE_BLOB_UPLOAD_RESULT_41_001: [If `err` is `null` and `uploadResponse` is falsy a `ReferenceError` shall be thrown]*/
    if (!err && !uploadResponse) { throw new ReferenceError('err and uploadResponse cannot both be null'); }
    let uploadResult: BlobUploadResult;
    if (err) {
      const statusCode = ((Object.prototype.hasOwnProperty.call(err, 'statusCode')) ? (err.statusCode) : (-1));
      const statusDescription = `Failed to upload to blob${statusCode === -1 ? '' : `. Status code: ${statusCode}`}`;
      /*Codes_SRS_NODE_DEVICE_BLOB_UPLOAD_RESULT_41_002: [If `err` is not `null`, the `BlobUploadResult` shall have the `isSuccess` property set to `false`]*/
      uploadResult = new BlobUploadResult(false, statusCode, statusDescription);
    } else {
      if (uploadResponse.errorCode && uploadResponse.errorCode) {
        /*Codes_SRS_NODE_DEVICE_BLOB_UPLOAD_RESULT_41_003: [If `err` is null but `uploadResponse` is provided, and `uploadResponse.ErrorCode` is not null, `BlobUploadResult` shall have the `isSuccess` property set to `false`]*/
        const statusCode = uploadResponse._response ? uploadResponse._response.status : -1;
        const statusDescription = uploadResponse._response ? uploadResponse._response.bodyAsText : 'no status description';
        uploadResult = new BlobUploadResult(false, statusCode, statusDescription);
      } else {
        /*Codes_SRS_NODE_DEVICE_BLOB_UPLOAD_RESULT_41_004: [If `err` is null and `uploadResponse` is provided, the `BlobUploadResult` shall have the `statusCode` and `statusDescription` property set to the HTTP status code of the blob upload response]*/
        const statusCode = uploadResponse._response.status;
        const statusDescription = uploadResponse._response.bodyAsText;
        if (uploadResponse._response.status >= 200 && uploadResponse._response.status < 300) {
            /*Codes_SRS_NODE_DEVICE_BLOB_UPLOAD_RESULT_41_005: [If `uploadResponse._response.status` is a number within the HTTP Status Codes 'success' range, the `isSuccess` property will be set to `true`]*/
            uploadResult = new BlobUploadResult(true, statusCode, statusDescription);
          } else {
            /*Codes_SRS_NODE_DEVICE_BLOB_UPLOAD_RESULT_41_006: [If `uploadResponse._response.status` is a number not in the HTTP Status Codes 'success' range, the `isSuccess` property will be set to `false`]*/
            uploadResult = new BlobUploadResult(false, statusCode, statusDescription);
          }
      }
    }
    return uploadResult;
  }