Nettoyer façon d'erreurs de prise de Azure Table (autre que string match?)
-
03-10-2019 - |
Question
Je voudrais obtenir une liste de toutes les erreurs de table Azure et trouver un moyen propre pour les manipuler dans un bloc de try...catch
.
Par exemple, j'aimerais pas au code directement et comparer le message InnerException à String.Contains("The specified entity already exists")
. Quelle est la bonne façon de piéger ces erreurs?
La solution 4
Voici le code qui est fourni dans le Azure Table Livre blanc, mais je ne suis pas sûr si cela donne une valeur sur la réponse de 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;
}
}
}
Autres conseils
Vous pouvez essayer de regarder les valeurs dans la réponse, plutôt que l'exception intérieure. Ceci est un exemple d'un de mes essayages blocs catch:
try {
return query.FirstOrDefault();
}
catch (System.Data.Services.Client.DataServiceQueryException ex)
{
if (ex.Response.StatusCode == (int)System.Net.HttpStatusCode.NotFound) {
return null;
}
throw;
}
Il est évident que cela est juste pour l'élément n'existe pas d'erreur, mais je suis sûr que vous pouvez développer ce concept en regardant le liste des codes d'erreur Azure .
Pour gérer les erreurs tout en ajoutant des objets à une table, vous pouvez utiliser le code suivant:
try {
_context.AddObject(TableName, entityObject);
_context.SaveCangesWithRetries();
}
catch(DataServiceRequestException ex) {
ex.Response.Any(r => r.StatusCode == (int)System.Net.HttpStatusCode.Conflict)
throw;
}
Comme dit dans un autre réponse que vous pouvez trouver une liste des erreurs à TableStorage: http : //msdn.microsoft.com/en-us/library/dd179438.aspx
Voir mon code ici: http: // blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob . Le modèle est d'attraper un StorageClientException, puis utiliser la propriété .ErrorCode pour correspondre contre les constantes dans StorageErrorCode.