/*******************************************************************************
* Copyright 2008-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file.
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* *****************************************************************************
* __ _ _ ___
* ( )( \/\/ )/ __)
* /__\ \ / \__ \
* (_)(_) \/\/ (___/
*
* AWS SDK for .NET
*
*/
using System;
using System.Collections.Generic;
using System.Net;
using System.Xml;
using System.Xml.Serialization;
namespace Amazon.S3.Util
{
///
/// An exception detailing a failed HTTP POST upload atempt to Amazon S3.
///
#if !PCL
[Serializable]
#endif
public class S3PostUploadException : Exception
{
///
/// Initializes a new instance of S3PostUploadException with a specified error message
///
/// The error message
public S3PostUploadException(string message) : base(message) {}
///
/// Initializes a new instance of S3PostUploadException with a specified error code and error message
///
/// The error code
/// The error message
public S3PostUploadException(string errorCode, string message) : base(message)
{
this.ErrorCode = errorCode;
}
///
/// The error code returned by S3
///
public string ErrorCode { get; set; }
///
/// The S3 request id
///
public string RequestId { get; set; }
///
/// The S3 host id
///
public string HostId { get; set; }
///
/// The HTTP error status code returned by S3
///
public HttpStatusCode StatusCode { get; set; }
///
/// Additional information about the error
///
///
/// Some errors are accompanied by more specific information, which vary from error to error
///
public IDictionary ExtraFields { get; set; }
///
/// Parse an S3 Error response and create an S3PostUploadException
///
/// The response from S3
/// An S3PostUploadException with the information from the response
public static S3PostUploadException FromResponse(HttpWebResponse response)
{
var serializer = new XmlSerializer(typeof(S3PostUploadError));
S3PostUploadError err = null;
try
{
err = serializer.Deserialize(response.GetResponseStream()) as S3PostUploadError;
}
catch
{
return new S3PostUploadException("Unknown", "Unknown error");
}
var ex = new S3PostUploadException(err.ErrorCode, err.ErrorMessage)
{
RequestId = err.RequestId,
HostId = err.HostId,
};
ex.StatusCode = response.StatusCode;
ex.ExtraFields = new Dictionary();
if (err.elements != null)
{
foreach (var f in err.elements)
{
ex.ExtraFields.Add(f.LocalName, f.InnerText);
}
}
return ex;
}
#if !PCL
///
/// Constructs a new instance of the S3PostUploadException class with serialized data.
///
/// The that holds the serialized object data about the exception being thrown.
/// The that contains contextual information about the source or destination.
/// The parameter is null.
/// The class name is null or is zero (0).
protected S3PostUploadException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
: base(info, context)
{
if (info != null)
{
this.ErrorCode = info.GetString("ErrorCode");
this.RequestId = info.GetString("RequestId");
this.HostId = info.GetString("HostId");
this.StatusCode = (HttpStatusCode)info.GetValue("StatusCode", typeof(HttpStatusCode));
this.ExtraFields = (IDictionary)info.GetValue("ExtraFields", typeof(IDictionary));
}
}
///
/// Sets the with information about the exception.
///
/// The that holds the serialized object data about the exception being thrown.
/// The that contains contextual information about the source or destination.
/// The parameter is a null reference (Nothing in Visual Basic).
#if BCL35
[System.Security.Permissions.SecurityPermission(
System.Security.Permissions.SecurityAction.LinkDemand,
Flags = System.Security.Permissions.SecurityPermissionFlag.SerializationFormatter)]
#endif
[System.Security.SecurityCritical]
//// These FxCop rules are giving false-positives for this method
//[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
//[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2134:MethodsMustOverrideWithConsistentTransparencyFxCopRule")]
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
base.GetObjectData(info, context);
if (info != null)
{
info.AddValue("ErrorCode", this.ErrorCode);
info.AddValue("RequestId", this.RequestId);
info.AddValue("HostId", this.HostId);
info.AddValue("StatusCode", this.StatusCode);
info.AddValue("ExtraFields", this.ExtraFields);
}
}
#endif
}
///
/// Class for unmarshalling response XML
///
[XmlRoot("Error")]
public class S3PostUploadError
{
///
/// Gets and sets the ErrorCode property.
///
[XmlElement("Code")]
public string ErrorCode { get; set; }
///
/// Gets and sets the ErrorMessage property.
///
[XmlElement("Message")]
public string ErrorMessage { get; set; }
///
/// Gets and sets the RequestId property.
///
[XmlElement("RequestId")]
public string RequestId { get; set; }
///
/// Gets and sets the HostId property.
///
[XmlElement("HostId")]
public string HostId { get; set; }
///
/// Gets and sets the elements property.
///
[XmlAnyElement()]
public XmlElement[] elements { get; set; }
}
}