Frage

Ich mag eine Liste aller Azure Table Fehler und Figur aus einem sauberen Weg, um sie in einem try...catch Block zu behandeln.

Zum Beispiel, würde Ich mag nicht direkt Code haben und die Innerexception Nachricht an String.Contains("The specified entity already exists") zu vergleichen. Was ist der richtige Weg, diese Fehler zu stoppen?

alt text

War es hilfreich?

Lösung 4

Hier ist Code, der in der Azure Table White Paper , aber ich ‚m nicht sicher, ob dies einen Wert über smark Antwort gibt.

   /*
         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;
            }
        }
    }

Andere Tipps

Sie könnten versuchen, auf die Werte in der Antwort suchen, sondern dass die innere Ausnahme. Dies ist ein Beispiel von einem meiner Try-Catch-Blöcken:

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

Natürlich ist dies nur für das Element nicht Fehler vorhanden ist, aber ich bin sicher, dass Sie auf diesem Konzept auf dem Liste der Azure-Fehlercodes .

Um Fehler zu handhaben, während Objekte zu einer Tabelle hinzufügen, die Sie den folgenden Code verwenden:

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

, sagte Wie in anderer Antwort eine Liste von TableStorage Fehlern bei finden: http : //msdn.microsoft.com/en-us/library/dd179438.aspx

Sehen Sie meinen Code hier: http: // blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob . Das Muster ist ein StorageClientException zu fangen, und dann die .ErrorCode Eigenschaft gegen die Konstanten in StorageErrorCode entspricht.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top