azure-sdk-for-go icon indicating copy to clipboard operation
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

Open ArnavPrasadMicrosoft opened this issue 3 years ago • 3 comments

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

ArnavPrasadMicrosoft avatar Feb 05 '22 04:02 ArnavPrasadMicrosoft

Looks related to #16722

chlowell avatar Feb 07 '22 16:02 chlowell

Yes, and related issue was closed due to inactivity :(

ezavgorodniy avatar Feb 21 '22 15:02 ezavgorodniy

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)
  }

joey-laminar avatar Jun 06 '22 07:06 joey-laminar

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.

tasherif-msft avatar Sep 06 '22 15:09 tasherif-msft

This is correct. However, we haven't yet released an updated azblob that returns this error type.

jhendrixMSFT avatar Sep 06 '22 16:09 jhendrixMSFT

@jhendrixMSFT you're correct, I have reopened the issue and updated my comment that this change should be available in the next release

tasherif-msft avatar Sep 06 '22 16:09 tasherif-msft

This change has now been deployed in [email protected]

jhendrixMSFT avatar Sep 29 '22 21:09 jhendrixMSFT