示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StorageServerException"/> class.
 /// </summary>
 /// <param name="errorCode">The storage client error code.</param>
 /// <param name="message">The message that describes the exception.</param>
 /// <param name="statusCode">The HTTP status code returned in the response.</param>
 /// <param name="innerException">The <see cref="Exception"/> instance that caused the current exception.</param>
 internal StorageServerException(
     StorageErrorCode errorCode,
     string message,
     HttpStatusCode statusCode,
     Exception innerException)
     : base(errorCode, message, statusCode, null, innerException)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="StorageServerException"/> class.
 /// </summary>
 /// <param name="errorCode">The storage client error code.</param>
 /// <param name="message">The message that describes the exception.</param>
 /// <param name="statusCode">The HTTP status code returned in the response.</param>
 /// <param name="innerException">The <see cref="Exception"/> instance that caused the current exception.</param>
 internal StorageServerException(
     StorageErrorCode errorCode,
     string message,
     HttpStatusCode statusCode,
     Exception innerException)
     : base(errorCode, message, statusCode, null, innerException)
 {
 }
 /// <summary>Initializes a new instance of the <see cref="StorageServerException"/> class.</summary>
 /// <param name="errorCode">The storage client error code. </param>
 /// <param name="message">The message that describes the exception. </param>
 /// <param name="statusCode">The HTTP status code returned in the response. </param>
 /// <param name="extendedErrorInfo">The extended error information. </param>
 /// <param name="innerException">The <see cref="Exception"/> instance that caused the current exception. </param>
 internal StorageServerException(
     StorageErrorCode errorCode,
     string message,
     HttpStatusCode statusCode,
     StorageExtendedErrorInformation extendedErrorInfo,
     Exception innerException)
     : base(errorCode, message, statusCode, extendedErrorInfo, innerException)
 {
 }
示例#4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StorageServerException"/> class.
 /// </summary>
 /// <param name="errorCode">The storage client error code.</param>
 /// <param name="message">The message that describes the exception.</param>
 /// <param name="statusCode">The HTTP status code returned in the response.</param>
 /// <param name="extendedErrorInfo">The extended error information.</param>
 /// <param name="innerException">The <see cref="Exception"/> instance that caused the current exception.</param>
 internal StorageServerException(
     StorageErrorCode errorCode,
     string message,
     HttpStatusCode statusCode,
     StorageExtendedErrorInformation extendedErrorInfo,
     Exception innerException)
     : base(errorCode, message, statusCode, extendedErrorInfo, innerException)
 {
 }
示例#5
0
 public static StorageError ToStorageError(this StorageErrorCode code)
 {
     return(Errors
            .Where(x => x.Key == (int)code)
            .Select(x => new StorageError {
         Code = x.Key, Message = x.Value
     })
            .FirstOrDefault());
 }
 /// <summary>Initializes a new instance of the <see cref="StorageException"/> class.</summary>
 /// <param name="errorCode">The storage client error code. </param>
 /// <param name="message">The message that describes the exception. </param>
 /// <param name="statusCode">The HTTP status code returned in the response. </param>
 /// <param name="extendedErrorInfo">The extended error information. </param>
 /// <param name="innerException">The <see cref="Exception"/> instance that caused the current exception. </param>
 protected StorageException(
     StorageErrorCode errorCode,
     string message,
     HttpStatusCode statusCode,
     StorageExtendedErrorInformation extendedErrorInfo,
     Exception innerException)
     : base(message, innerException)
 {
     this.ErrorCode = errorCode;
     this.StatusCode = statusCode;
     this.ExtendedErrorInformation = extendedErrorInfo;
 }
示例#7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StorageException"/> class.
 /// </summary>
 /// <param name="errorCode">The storage client error code.</param>
 /// <param name="message">The message that describes the exception.</param>
 /// <param name="statusCode">The HTTP status code returned in the response.</param>
 /// <param name="extendedErrorInfo">The extended error information.</param>
 /// <param name="innerException">The <see cref="Exception"/> instance that caused the current exception.</param>
 protected StorageException(
     StorageErrorCode errorCode,
     string message,
     HttpStatusCode statusCode,
     StorageExtendedErrorInformation extendedErrorInfo,
     Exception innerException)
     : base(message, innerException)
 {
     this.ErrorCode  = errorCode;
     this.StatusCode = statusCode;
     this.ExtendedErrorInformation = extendedErrorInfo;
 }
        public static StorageError ToStorageError(this StorageErrorCode code)
        {
            var error = Errors
                        .Where(x => x.Key == code)
                        .Select(x => new StorageError()
            {
                Code = (int)x.Key, Message = x.Value
            })
                        .FirstOrDefault();

            return(error ?? new StorageError
            {
                Code = (int)StorageErrorCode.GenericException,
                Message = "Generic provider exception occurred",
            });
        }
示例#9
0
 public StorageException(string message, StorageErrorCode errorCode)
     : base(message)
 {
     ErrorCode = errorCode;
 }
示例#10
0
        private static Exception TranslateExtendedError(
                    StorageExtendedErrorInformation details,
                    HttpStatusCode statusCode,
                    string statusDescription,
                    Exception inner
                    )
        {
            StorageErrorCode errorCode = default(StorageErrorCode);
            switch (details.ErrorCode)
            {
                case StorageErrorCodeStrings.UnsupportedHttpVerb:
                case StorageErrorCodeStrings.MissingContentLengthHeader:
                case StorageErrorCodeStrings.MissingRequiredHeader:
                case StorageErrorCodeStrings.UnsupportedHeader:
                case StorageErrorCodeStrings.InvalidHeaderValue:
                case StorageErrorCodeStrings.MissingRequiredQueryParameter:
                case StorageErrorCodeStrings.UnsupportedQueryParameter:
                case StorageErrorCodeStrings.InvalidQueryParameterValue:
                case StorageErrorCodeStrings.OutOfRangeQueryParameterValue:
                case StorageErrorCodeStrings.InvalidUri:
                case StorageErrorCodeStrings.InvalidHttpVerb:
                case StorageErrorCodeStrings.EmptyMetadataKey:
                case StorageErrorCodeStrings.RequestBodyTooLarge:
                case StorageErrorCodeStrings.InvalidXmlDocument:
                case StorageErrorCodeStrings.InvalidXmlNodeValue:
                case StorageErrorCodeStrings.MissingRequiredXmlNode:
                case StorageErrorCodeStrings.InvalidMd5:
                case StorageErrorCodeStrings.OutOfRangeInput:
                case StorageErrorCodeStrings.InvalidInput:
                case StorageErrorCodeStrings.InvalidMetadata:
                case StorageErrorCodeStrings.MetadataTooLarge:
                case StorageErrorCodeStrings.InvalidRange:
                    errorCode = StorageErrorCode.BadRequest;
                    break;
                case StorageErrorCodeStrings.AuthenticationFailed:
                    errorCode = StorageErrorCode.AuthenticationFailure;
                    break;
                case StorageErrorCodeStrings.ResourceNotFound:
                    errorCode = StorageErrorCode.ResourceNotFound;
                    break;
                case StorageErrorCodeStrings.ConditionNotMet:
                    errorCode = StorageErrorCode.ConditionFailed;
                    break;
                case StorageErrorCodeStrings.ContainerAlreadyExists:
                    errorCode = StorageErrorCode.ContainerAlreadyExists;
                    break;
                case StorageErrorCodeStrings.ContainerNotFound:
                    errorCode = StorageErrorCode.ContainerNotFound;
                    break;
                case BlobErrorCodeStrings.BlobNotFound:
                    errorCode = StorageErrorCode.BlobNotFound;
                    break;
                case BlobErrorCodeStrings.BlobAlreadyExists:
                    errorCode = StorageErrorCode.BlobAlreadyExists;
                    break;
            }

            if (errorCode != default(StorageErrorCode))
                return new StorageClientException(
                                errorCode,
                                statusDescription,
                                statusCode,
                                details,
                                inner
                                );

            switch (details.ErrorCode)
            {
                case StorageErrorCodeStrings.InternalError:
                case StorageErrorCodeStrings.ServerBusy:
                    errorCode = StorageErrorCode.ServiceInternalError;
                    break;
                case StorageErrorCodeStrings.Md5Mismatch:
                    errorCode = StorageErrorCode.ServiceIntegrityCheckFailed;
                    break;
                case StorageErrorCodeStrings.OperationTimedOut:
                    errorCode = StorageErrorCode.ServiceTimeout;
                    break;
            }
            if (errorCode != default(StorageErrorCode))
                return new StorageServerException(
                                errorCode,
                                statusDescription,
                                statusCode,
                                details,
                                inner
                                );



            return null;
        }
 public StorageException(StorageErrorCode errorCode, string message)
     : base(message)
 {
     ErrorCode = (int)errorCode;
 }
 public StorageException(StorageErrorCode errorCode)
     : this(errorCode, (Exception)null)
 {
 }
 public StorageException(StorageErrorCode errorCode, Exception ex)
     : base(errorCode.ToStorageError().Message, ex)
 {
     ErrorCode       = (int)errorCode;
     ProviderMessage = ex?.Message;
 }
示例#14
0
        /// <summary>
        /// Puts a blob into the underlying storage and returns its eTag value.
        /// If the blob with the same name already exists, overwrite behavior can be applied.
        /// </summary>
        /// <typeparam name="T">The type of the payload associated with the blob.</typeparam>
        /// <param name="containerName">The target blob container name into which a blob will be stored.</param>
        /// <param name="blobName">The custom name associated with the blob.</param>
        /// <param name="blob">The blob's payload.</param>
        /// <param name="overwrite">The flag indicating whether or not overwriting the existing blob is permitted.</param>
        /// <param name="expectedEtag">The parameters holding the blob's expected eTag value.</param>
        /// <param name="actualEtag">The output parameters holding the blob's actual eTag value.</param>
        /// <returns>True if the blob was successfully put into the specified container, otherwise false.</returns>
        private bool Put <T>(string containerName, string blobName, T blob, bool overwrite, string expectedEtag, out string actualEtag)
        {
            Guard.ArgumentNotNullOrEmptyString(containerName, "containerName");
            Guard.ArgumentNotNullOrEmptyString(blobName, "blobName");
            Guard.ArgumentNotNull(blob, "blob");

            var callToken = TraceManager.CloudStorageComponent.TraceIn(containerName, blobName, overwrite, expectedEtag);

            // Verify whether or not the specified blob is already of type Stream.
            Stream blobStream = IsStreamType(blob.GetType()) ? blob as Stream : null;
            Stream blobData   = null;

            actualEtag = null;

            try
            {
                // Are we dealing with a stream already? If yes, just use it as is.
                if (blobStream != null)
                {
                    blobData = blobStream;
                }
                else
                {
                    // The specified blob is something else rather than a Stream, we perform serialization of T into a new stream instance.
                    blobData = new MemoryStream();
                    this.dataSerializer.Serialize(blob, blobData);
                }

                var container = this.blobStorage.GetContainerReference(CloudUtility.GetSafeContainerName(containerName));
                StorageErrorCode lastErrorCode = StorageErrorCode.None;

                Func <string> uploadAction = () =>
                {
                    var cloudBlob = container.GetBlobReference(blobName);
                    return(UploadBlob(cloudBlob, blobData, overwrite, expectedEtag));
                };

                try
                {
                    // First attempt - perform upload and let the UploadBlob method handle any retry conditions.
                    string eTag = uploadAction();

                    if (!String.IsNullOrEmpty(eTag))
                    {
                        actualEtag = eTag;
                        return(true);
                    }
                }
                catch (StorageClientException ex)
                {
                    lastErrorCode = ex.ErrorCode;

                    if (!(lastErrorCode == StorageErrorCode.ContainerNotFound || lastErrorCode == StorageErrorCode.ResourceNotFound || lastErrorCode == StorageErrorCode.BlobAlreadyExists))
                    {
                        // Anything other than "not found" or "already exists" conditions will be considered as a runtime error.
                        throw;
                    }
                }

                if (lastErrorCode == StorageErrorCode.ContainerNotFound)
                {
                    // Failover action #1: create the target container and try again. This time, use a retry policy to wrap calls to the UploadBlob method.
                    string eTag = this.retryPolicy.ExecuteAction <string>(() =>
                    {
                        CreateContainer(containerName);
                        return(uploadAction());
                    });

                    return(!String.IsNullOrEmpty(actualEtag = eTag));
                }

                if (lastErrorCode == StorageErrorCode.BlobAlreadyExists && overwrite)
                {
                    // Failover action #2: Overwrite was requested but BlobAlreadyExists has still been returned. Delete the original blob and try to upload again.
                    string eTag = this.retryPolicy.ExecuteAction <string>(() =>
                    {
                        var cloudBlob = container.GetBlobReference(blobName);
                        cloudBlob.DeleteIfExists();

                        return(uploadAction());
                    });

                    return(!String.IsNullOrEmpty(actualEtag = eTag));
                }
            }
            finally
            {
                // Only dispose the blob data stream if it was newly created.
                if (blobData != null && null == blobStream)
                {
                    blobData.Dispose();
                }

                TraceManager.CloudStorageComponent.TraceOut(callToken, actualEtag);
            }

            return(false);
        }
 public StorageException(StorageErrorCode errorCode, Exception ex)
     : base(errorCode.ToStorageError().Message, ex)
 {
     ErrorCode = (int) errorCode;
     ProviderMessage = ex?.Message;
 }
示例#16
0
 /// <summary>
 /// 根据错误类型返回错误异常
 /// </summary>
 /// <param name="code"></param>
 /// <returns></returns>
 public static StorageError ToStorageError(this StorageErrorCode code) => new StorageError()
 {
     Code    = (int)code,
     Message = code.GetDisplayContent()
 };