Exemplo n.º 1
0
        internal static Exception TranslateFromHttpStatus(
            HttpStatusCode statusCode,
            string statusDescription,
            StorageExtendedErrorInformation details,
            Exception inner
            )
        {
            switch (statusCode)
            {
            case HttpStatusCode.Forbidden:
                return(new StorageClientException(
                           StorageErrorCode.AccessDenied,
                           statusDescription,
                           HttpStatusCode.Forbidden,
                           details,
                           inner
                           ));

            case HttpStatusCode.Gone:
            case HttpStatusCode.NotFound:
                return(new StorageClientException(
                           StorageErrorCode.ResourceNotFound,
                           statusDescription,
                           statusCode,
                           details,
                           inner
                           ));

            case HttpStatusCode.BadRequest:
                return(new StorageClientException(
                           StorageErrorCode.BadRequest,
                           statusDescription,
                           statusCode,
                           details,
                           inner
                           ));

            case HttpStatusCode.PreconditionFailed:
            case HttpStatusCode.NotModified:
                return(new StorageClientException(
                           StorageErrorCode.BadRequest,
                           statusDescription,
                           statusCode,
                           details,
                           inner));

            case HttpStatusCode.Conflict:
                return(new StorageClientException(
                           StorageErrorCode.ResourceAlreadyExists,
                           statusDescription,
                           statusCode,
                           details,
                           inner
                           ));

            case HttpStatusCode.GatewayTimeout:
                return(new StorageServerException(
                           StorageErrorCode.ServiceTimeout,
                           statusDescription,
                           statusCode,
                           details,
                           inner
                           ));

            case HttpStatusCode.RequestedRangeNotSatisfiable:
                return(new StorageClientException(
                           StorageErrorCode.BadRequest,
                           statusDescription,
                           statusCode,
                           details,
                           inner
                           ));

            case HttpStatusCode.InternalServerError:
                return(new StorageServerException(
                           StorageErrorCode.ServiceInternalError,
                           statusDescription,
                           statusCode,
                           details,
                           inner
                           ));

            case HttpStatusCode.BadGateway:
                return(new StorageServerException(
                           StorageErrorCode.BadGateway,
                           statusDescription,
                           statusCode,
                           details,
                           inner
                           ));
            }
            return(null);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        internal static Exception TranslateWebException(Exception e)
        {
            WebException we = e as WebException;

            if (null == we)
            {
                return(e);
            }

            // If the response is not null, let's first see what the status code is.
            if (we.Response != null)
            {
                HttpWebResponse response = ((HttpWebResponse)we.Response);

                StorageExtendedErrorInformation extendedError =
                    GetExtendedErrorDetailsFromResponse(
                        response.GetResponseStream(),
                        response.ContentLength
                        );
                Exception translatedException = null;
                if (extendedError != null)
                {
                    translatedException = TranslateExtendedError(
                        extendedError,
                        response.StatusCode,
                        response.StatusDescription,
                        e);
                    if (translatedException != null)
                    {
                        return(translatedException);
                    }
                }
                translatedException = TranslateFromHttpStatus(
                    response.StatusCode,
                    response.StatusDescription,
                    extendedError,
                    we
                    );
                if (translatedException != null)
                {
                    return(translatedException);
                }
            }

            switch (we.Status)
            {
            case WebExceptionStatus.RequestCanceled:
                return(new StorageServerException(
                           StorageErrorCode.ServiceTimeout,
                           "The server request did not complete within the specified timeout",
                           HttpStatusCode.GatewayTimeout,
                           we));

            case WebExceptionStatus.ConnectFailure:
                return(we);

            default:
                return(new StorageServerException(
                           StorageErrorCode.ServiceInternalError,
                           "The server encountered an unknown failure: " + e.Message,
                           HttpStatusCode.InternalServerError,
                           we
                           ));
            }
        }