protected StorageException( StorageErrorCode errorCode, string message, HttpStatusCode statusCode, StorageExtendedErrorInformation extendedErrorInfo, Exception innerException ) : base(message, innerException) { this.ErrorCode = errorCode; this.StatusCode = statusCode; this.ExtendedErrorInformation = extendedErrorInfo; }
private static StorageExtendedErrorInformation GetErrorDetailsFromStream( Stream inputStream ) { StorageExtendedErrorInformation extendedError = new StorageExtendedErrorInformation(); try { using (XmlReader reader = XmlReader.Create(inputStream)) { reader.Read(); reader.ReadStartElement(StorageHttpConstants.XmlElementNames.ErrorRootElement); extendedError.ErrorCode = reader.ReadElementString(StorageHttpConstants.XmlElementNames.ErrorCode); extendedError.ErrorMessage = reader.ReadElementString(StorageHttpConstants.XmlElementNames.ErrorMessage); extendedError.AdditionalDetails = new NameValueCollection(); // After error code and message we can have a number of additional details optionally followed // by ExceptionDetails element - we'll read all of these into the additionalDetails collection do { if (reader.IsStartElement()) { if (string.Compare(reader.LocalName, StorageHttpConstants.XmlElementNames.ErrorException, StringComparison.Ordinal) == 0) { // Need to read exception details - we have message and stack trace reader.ReadStartElement(StorageHttpConstants.XmlElementNames.ErrorException); extendedError.AdditionalDetails.Add(StorageHttpConstants.XmlElementNames.ErrorExceptionMessage, reader.ReadElementString(StorageHttpConstants.XmlElementNames.ErrorExceptionMessage)); extendedError.AdditionalDetails.Add(StorageHttpConstants.XmlElementNames.ErrorExceptionStackTrace, reader.ReadElementString(StorageHttpConstants.XmlElementNames.ErrorExceptionStackTrace)); reader.ReadEndElement(); } else { string elementName = reader.LocalName; extendedError.AdditionalDetails.Add(elementName, reader.ReadString()); } } } while (reader.Read()); } } catch (XmlException) { //If there is a parsing error we cannot return extended error information return null; } return extendedError; }
internal static StorageExtendedErrorInformation GetExtendedErrorFromXmlMessage(string xmlErrorMessage) { string message = null; string errorCode = null; XName xnErrorCode = XName.Get(StorageHttpConstants.XmlElementNames.TableErrorCodeElement, StorageHttpConstants.XmlElementNames.DataWebMetadataNamespace); XName xnMessage = XName.Get(StorageHttpConstants.XmlElementNames.TableErrorMessageElement, StorageHttpConstants.XmlElementNames.DataWebMetadataNamespace); using (StringReader reader = new StringReader(xmlErrorMessage)) { XDocument xDocument = null; try { xDocument = XDocument.Load(reader); } catch (XmlException) { // The XML could not be parsed. This could happen either because the connection // could not be made to the server, or if the response did not contain the // error details (for example, if the response status code was neither a failure // nor a success, but a 3XX code such as NotModified. return null; } XElement errorCodeElement = xDocument.Descendants(xnErrorCode).FirstOrDefault(); if (errorCodeElement == null) return null; errorCode = errorCodeElement.Value; XElement messageElement = xDocument.Descendants(xnMessage).FirstOrDefault(); if (messageElement != null) { message = messageElement.Value; } } StorageExtendedErrorInformation errorDetails = new StorageExtendedErrorInformation(); errorDetails.ErrorMessage = message; errorDetails.ErrorCode = errorCode; return errorDetails; }
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; }
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; }
internal StorageClientException( StorageErrorCode errorCode, string message, HttpStatusCode statusCode, StorageExtendedErrorInformation extendedErrorInfo, Exception innerException ) : base(errorCode, message, statusCode, extendedErrorInfo, innerException) { }
/// <summary> /// Initializes a new instance of the <see cref="StorageException"/> class with /// serialized data. /// </summary> /// <param name="info">The <see cref="SerializationInfo"/> object that contains serialized object /// data about the exception being thrown</param> /// <param name="context">The <see cref="StreamingContext"/> object that contains contextual information /// about the source or destionation. </param> protected StorageException(SerializationInfo info, StreamingContext context) : base(info, context) { if (null == info) { throw new ArgumentNullException("info"); } this.StatusCode = (HttpStatusCode)info.GetValue("StatusCode", typeof(HttpStatusCode)); this.ErrorCode = (StorageErrorCode)info.GetValue("ErrorCode", typeof(StorageErrorCode)); this.ExtendedErrorInformation = (StorageExtendedErrorInformation)info.GetValue( "ExtendedErrorInformation", typeof(StorageExtendedErrorInformation)); }
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 ); } }