Azureテーブルからエラーをキャッチするためのクリーンな方法(文字列の一致以外?)

StackOverflow https://stackoverflow.com/questions/3739144

質問

すべてのAzureテーブルエラーのリストを取得し、それらを処理するためのきれいな方法を見つけたいと思います try...catch ブロック。

たとえば、innerexceptionメッセージを直接コーディングして比較する必要はありません。 String.Contains("The specified entity already exists"). 。これらのエラーをトラップする正しい方法は何ですか?

alt text

役に立ちましたか?

解決 4

で提供されるコードは次のとおりです Azureテーブルホワイトペーパー, 、しかし、これが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;
            }
        }
    }

他のヒント

内部の例外ではなく、応答の値を見てみることができます。これは、私のTry Catchブロックの1つの例です。

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

明らかに、これはアイテムが存在しないためだけですが、この概念を見ることで拡張できると確信しています Azureエラーコードのリスト.

テーブルにオブジェクトを追加しながらエラーを処理するには、次のコードを使用できます。

try {
  _context.AddObject(TableName, entityObject);
  _context.SaveCangesWithRetries(); 
}
catch(DataServiceRequestException ex) {
  ex.Response.Any(r => r.StatusCode == (int)System.Net.HttpStatusCode.Conflict) 
  throw;
}

他の回答で述べたように、次のような象徴的なエラーのリストを見つけることができます。 http://msdn.microsoft.com/en-us/library/dd179438.aspx

ここで私のコードを参照してください: http://blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob. 。パターンは、StorageClientExceptionをキャッチし、.ErrorCodeプロパティを使用してStorageErrorCodeの定数と一致させることです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top