【问题标题】:Clean way to catch errors from Azure Table (other than string match?)从 Azure 表中捕获错误的干净方法(字符串匹配除外?)
【发布时间】:2010-09-17 21:24:34
【问题描述】:

我想获取所有 Azure Table 错误的列表,并在 try...catch 块中找到一种干净的方法来处理它们。

例如,我不想直接编写代码并将 InnerException 消息与String.Contains("The specified entity already exists") 进行比较。捕获这些错误的正确方法是什么?

【问题讨论】:

    标签: c# azure try-catch azure-table-storage


    【解决方案1】:

    您可以尝试查看响应中的值,而不是内部异常。这是我的一个 try catch 块的示例:

    try {
        return query.FirstOrDefault();
    }
    catch (System.Data.Services.Client.DataServiceQueryException ex)
    {
        if (ex.Response.StatusCode == (int)System.Net.HttpStatusCode.NotFound) {
            return null;
        }
        throw;
    }
    

    显然这只是因为 item doesn't exist 错误,但我相信您可以通过查看 list of Azure error codes 来扩展此概念。

    【讨论】:

      【解决方案2】:

      要在向表中添加对象时处理错误,您可以使用以下代码:

      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

      【讨论】:

        【解决方案3】:

        在此处查看我的代码:http://blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob。该模式是捕获一个 StorageClientException,然后使用 .ErrorCode 属性来匹配 StorageErrorCode 中的常量。

        【讨论】:

        • 谢谢!哪些异常通常与:context.RetryPolicy 相关联? ..超时? ..Expect100 继续? ..UsePostTunneling? ...合并选项? ...服务点。连接限制?在创建强大的应用程序时,指南会很有帮助。
        • 似乎 Table 会抛出“System.Data.Services.Client.DataServiceQueryException”而不是您博客中提到的“StorageClientException”。这将改变我的处理程序实现......我还不确定。
        【解决方案4】:

        这是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;
                    }
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-11-02
          • 2019-02-10
          • 2015-06-19
          • 1970-01-01
          • 2012-04-20
          • 1970-01-01
          • 2012-02-01
          • 2023-01-12
          相关资源
          最近更新 更多