I'm migrating from WindowsAzure.StorageClient 1.7 to WindowsAzure.Storage 2.0, and I'm working right now on the management of the exceptions. Following this guide and other sources, I found out I had to migrate from
try
{
// Something
}
catch (StorageClientException e)
{
switch (e.ErrorCode)
{
case StorageErrorCode.ContainerNotFound:
case StorageErrorCode.ResourceNotFound:
case StorageErrorCode.BlobNotFound:
case StorageErrorCode.ConditionFailed:
// Do something
}
}
to
try
{
// Something
}
catch (StorageException e)
{
switch (e.RequestInformation.ExtendedErrorInformation.ErrorCode)
{
case StorageErrorCodeStrings.ContainerNotFound:
case StorageErrorCodeStrings.ResourceNotFound:
case BlobErrorCodeStrings.BlobNotFound:
case StorageErrorCodeStrings.ConditionNotMet:
// Do something
}
}
Looks simple.
The problem is ExtendedErrorInformation is always equal to null. The HttpStatusMessage instead says 'The specified blob does not exist.', as it should.
I thought it was caused by the simulator of the test environment, but trying it in a real Azure environment drived me to the same situation.
Any idea?
Another option is to look at the RequestInformation.HttpStatusCode
instead. This seems to be more reliable anyway. Your code translates rather easily to:
try
{
// Something
}
catch (StorageException e)
{
switch (e.RequestInformation.HttpStatusCode)
{
case (int)HttpStatusCode.NotFound:
case (int)HttpStatusCode.PreconditionFailed:
// Do something
}
}
I just tried it and was surprised to see that indeed the ExtendedErrorInformation object is null. However it's not always null. For example, if I try to create a blob container which already exists using blobContainer.Create() method, I will get a non-null ExtendedErrorInformation. However if I try and fetch attributes of a blob which does not exist in the blob container, I will get a null ExtendedErrorInformation object. I guess one can't just assume that ExtendedErrorInformation object will always be available.
Also I noticed that in your code for 2.0, you're using StorageErrorCodeStrings. Please note that it is removed from 2.0 and is available only available with version 1.8 or before. Thought I should mention that
Update: Please see comment below from @VollmonD. This has been added in version 2.0.3.
Late to the party, but if you are trying to handle deleting items from the blob or just checking if they exist (extension method kinda approach).
you can now use:
CloudBlob.DeleteIfExistsAsync() - https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblockblob.deleteifexistsasync.aspx
CloudBlob.ExistsAsync() - https://msdn.microsoft.com/en-us/library/mt423366.aspx
Check here for a list of methods on CloudBlob: https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblockblob.aspx