我想获得所有Azure表错误的列表,并找出一个干净的方法来处理它们在try...catch
块。干净的方式来从Azure表中捕获错误(除字符串匹配?)
例如,我想不必直接编码并将InnerException消息与String.Contains("The specified entity already exists")
进行比较。捕捉这些错误的正确方法是什么?
我想获得所有Azure表错误的列表,并找出一个干净的方法来处理它们在try...catch
块。干净的方式来从Azure表中捕获错误(除字符串匹配?)
例如,我想不必直接编码并将InnerException消息与String.Contains("The specified entity already exists")
进行比较。捕捉这些错误的正确方法是什么?
以下是在Azure Table Whitepaper中提供的代码,但我不确定这是否给予smark的回复有任何价值。
/*
From Azure table whitepaper
When an exception occurs, you can extract the sequence number (highlighted above) of the command that caused the transaction to fail as follows:
try
{
// ... save changes
}
catch (InvalidOperationException e)
{
DataServiceClientException dsce = e.InnerException as DataServiceClientException;
int? commandIndex;
string errorMessage;
ParseErrorDetails(dsce, out commandIndex, out errorMessage);
}
*/
-
void ParseErrorDetails(DataServiceClientException e, out string errorCode, out int? commandIndex, out string errorMessage)
{
GetErrorInformation(e.Message, out errorCode, out errorMessage);
commandIndex = null;
int indexOfSeparator = errorMessage.IndexOf(':');
if (indexOfSeparator > 0)
{
int temp;
if (Int32.TryParse(errorMessage.Substring(0, indexOfSeparator), out temp))
{
commandIndex = temp;
errorMessage = errorMessage.Substring(indexOfSeparator + 1);
}
}
}
void GetErrorInformation( string xmlErrorMessage, out string errorCode, out string message)
{
message = null;
errorCode = null;
XName xnErrorCode = XName.Get("code", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
XName xnMessage = XName.Get ("message", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
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;
}
XElement errorCodeElement = xDocument.Descendants(xnErrorCode).FirstOrDefault();
if (errorCodeElement == null)
{
return;
}
errorCode = errorCodeElement.Value;
XElement messageElement = xDocument.Descendants(xnMessage).FirstOrDefault();
if (messageElement != null)
{
message = messageElement.Value;
}
}
}
你可以尝试寻找在响应中的值,而该内部异常。这是我尝试catch块的一个示例:
try {
return query.FirstOrDefault();
}
catch (System.Data.Services.Client.DataServiceQueryException ex)
{
if (ex.Response.StatusCode == (int)System.Net.HttpStatusCode.NotFound) {
return null;
}
throw;
}
显然,这只是项目不存在错误,但我敢肯定,你可以通过查看list of Azure error codes这个概念扩大。
看到我的代码在这里:http://blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob。该模式用于捕获StorageClientException,然后使用.ErrorCode属性与StorageErrorCode中的常量进行匹配。
要处理的错误,同时将对象添加到您可以使用下面的代码表:
try {
_context.AddObject(TableName, entityObject);
_context.SaveCangesWithRetries();
}
catch(DataServiceRequestException ex) {
ex.Response.Any(r => r.StatusCode == (int)System.Net.HttpStatusCode.Conflict)
throw;
}
正如其他回答说,你可以找到TableStorage列表错误在:http://msdn.microsoft.com/en-us/library/dd179438.aspx
谢谢!通常与以下相关的例外情况是:context.RetryPolicy? ..时间到? ..Expect100Continue? ..UsePostTunneling? ... MergeOption? ... servicepoint.ConnectionLimit?在创建强大的应用程序时,指南将非常有用。 – LamonteCristo 2010-09-18 20:39:26
似乎表将抛出“System.Data.Services.Client.DataServiceQueryException”而不是您的博客中提到的“StorageClientException”。这将改变我的处理程序实现...到什么,我还不确定。 – LamonteCristo 2010-09-19 18:14:24