azure-sdk-for-go
azure-sdk-for-go copied to clipboard
errors.As fails to match any error in the chain of *azblob.InternalError with azcore.HTTPResponse or azblob.StorageError
Bug Report
-
path of package in question:
https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/storage/azblob
-
SDK version:
0.2.0
-
output of
go version
: 1.16 -
What happened?
- I was trying to check for a "BlobAlreadyExists" error in my project through the following piece of code:
_, err = blockBlobClient.UploadBufferToBlockBlob(ctx, data, highLevelUploadToBlockBlobOption)
if err != nil {
// This log gets printed.
log.WithContext(ctx).Errorf("Error while uploading buffer to block blob: %v (%T)", err, err) // Type is (*azblob.InternalError)
var errResp azcore.HTTPResponse
var storageErr azblob.StorageError
if errors.As(err, &errResp) {
// This log does not get printed.
log.WithContext(ctx).Errorf("Caught response error: %v, status code: %d", errResp.RawResponse(), errResp.RawResponse().StatusCode)
}
if errors.As(err, &storageErr) {
// This log does not get printed either.
log.WithContext(ctx).Errorf("Caught storage error: %v", storageErr)
}
}
- Currently, the generated azblob code that actually sends requests returns azcore.HTTPResponse when something goes wrong, but the handwritten code converts that to another type which doesn't expose the response: https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/storage/azblob/zc_storage_error.go#L76
Looks related to #16722
Yes, and related issue was closed due to inactivity :(
Came upon this issue too, the solution was to define storageErr as a pointer:
var storageErr *azblob.StorageError
if errors.As(err, &storageErr) {
log.WithContext(ctx).Errorf("Caught storage error: %v", storageErr)
}
Hi @ArnavPrasadMicrosoft! Azure Core introduced https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/azcore/errors.go#L14 which can be cast using:
var responseErr *azcore.ResponseError
errors.As(err, &responseErr)
this will be available in the next release.
This is correct. However, we haven't yet released an updated azblob
that returns this error type.
@jhendrixMSFT you're correct, I have reopened the issue and updated my comment that this change should be available in the next release
This change has now been deployed in [email protected]