Domanda

Mi piacerebbe ottenere un elenco di tutti gli errori Azure Table e capire un modo pulito per gestirli in un blocco try...catch.

Per esempio, mi piacerebbe non dover direttamente il codice e confrontare il messaggio InnerException a String.Contains("The specified entity already exists"). Qual è il modo giusto per intercettare questi errori?

alt text

È stato utile?

Soluzione 4

Ecco il codice che viene fornito nella Azure Table Whitepaper , ma io 'm non è sicuro se questo dà un valore oltre la risposta di 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;
            }
        }
    }

Altri suggerimenti

Si potrebbe provare a guardare i valori nella risposta, piuttosto che l'eccezione interna. Questo è un esempio di uno dei miei blocchi catch provare:

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

Ovviamente questo è solo per la voce non esiste l'errore, ma sono sicuro che è possibile espandere su questo concetto, cercando in elenco dei codici di errore Azure .

Per gestire gli errori durante l'aggiunta di oggetti a un tavolo è possibile utilizzare il seguente codice:

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

Come detto in altra risposta si può trovare un elenco di TableStorage errori a: http : //msdn.microsoft.com/en-us/library/dd179438.aspx

Vedere il mio codice qui: http: // blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob . Il modello è quello di catturare uno StorageClientException, e quindi utilizzare la proprietà .ErrorCode per abbinare contro le costanti in StorageErrorCode.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top