Skip to content

Commit

Permalink
2023-11-03: avoid clobbering response errors, don't proceed to unmars…
Browse files Browse the repository at this point in the history
…hal/populate the response when an error has occurred
  • Loading branch information
manicminer committed Apr 22, 2024
1 parent 821fb79 commit 16f1fbd
Show file tree
Hide file tree
Showing 42 changed files with 438 additions and 354 deletions.
10 changes: 6 additions & 4 deletions storage/2023-11-03/blob/accounts/get_service_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ func (c Client) GetServiceProperties(ctx context.Context, accountName string) (r
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

err = resp.Unmarshal(&result)
if err != nil {
err = fmt.Errorf("unmarshalling response: %+v", err)
return
if err == nil {
err = resp.Unmarshal(&result)
if err != nil {
err = fmt.Errorf("unmarshalling response: %+v", err)
return
}
}
}
if err != nil {
Expand Down
26 changes: 14 additions & 12 deletions storage/2023-11-03/blob/blobs/append_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,21 @@ func (c Client) AppendBlock(ctx context.Context, containerName, blobName string,
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Header != nil {
result.BlobAppendOffset = resp.Header.Get("x-ms-blob-append-offset")
result.ContentMD5 = resp.Header.Get("Content-MD5")
result.ETag = resp.Header.Get("ETag")
result.LastModified = resp.Header.Get("Last-Modified")

if v := resp.Header.Get("x-ms-blob-committed-block-count"); v != "" {
i, innerErr := strconv.Atoi(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-blob-committed-block-count` header value %q: %+v", v, innerErr)
return
if err == nil {
if resp.Header != nil {
result.BlobAppendOffset = resp.Header.Get("x-ms-blob-append-offset")
result.ContentMD5 = resp.Header.Get("Content-MD5")
result.ETag = resp.Header.Get("ETag")
result.LastModified = resp.Header.Get("Last-Modified")

if v := resp.Header.Get("x-ms-blob-committed-block-count"); v != "" {
i, innerErr := strconv.Atoi(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-blob-committed-block-count` header value %q: %+v", v, innerErr)
return
}
result.BlobCommittedBlockCount = int64(i)
}
result.BlobCommittedBlockCount = int64(i)
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions storage/2023-11-03/blob/blobs/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ func (c Client) Copy(ctx context.Context, containerName, blobName string, input
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Header != nil {
result.CopyID = resp.Header.Get("x-ms-copy-id")
result.CopyStatus = resp.Header.Get("x-ms-copy-status")
if err == nil {
if resp.Header != nil {
result.CopyID = resp.Header.Get("x-ms-copy-id")
result.CopyStatus = resp.Header.Get("x-ms-copy-status")
}
}
}
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions storage/2023-11-03/blob/blobs/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ func (c Client) Get(ctx context.Context, containerName, blobName string, input G
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Body != nil {
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return result, fmt.Errorf("could not parse response body")
if err == nil {
if resp.Body != nil {
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return result, fmt.Errorf("could not parse response body")
}

result.Contents = &respBody
}

result.Contents = &respBody
}
}
if err != nil {
Expand Down
36 changes: 19 additions & 17 deletions storage/2023-11-03/blob/blobs/get_block_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,28 @@ func (c Client) GetBlockList(ctx context.Context, containerName, blobName string
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Header != nil {
result.ContentType = resp.Header.Get("Content-Type")
result.ETag = resp.Header.Get("ETag")

if v := resp.Header.Get("x-ms-blob-content-length"); v != "" {
i, innerErr := strconv.Atoi(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-blob-content-length` header value %q: %s", v, innerErr)
return
if err == nil {
if resp.Header != nil {
result.ContentType = resp.Header.Get("Content-Type")
result.ETag = resp.Header.Get("ETag")

if v := resp.Header.Get("x-ms-blob-content-length"); v != "" {
i, innerErr := strconv.Atoi(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-blob-content-length` header value %q: %s", v, innerErr)
return
}

i64 := int64(i)
result.BlobContentLength = &i64
}

i64 := int64(i)
result.BlobContentLength = &i64
}
}

err = resp.Unmarshal(&result)
if err != nil {
err = fmt.Errorf("unmarshalling response: %+v", err)
return
err = resp.Unmarshal(&result)
if err != nil {
err = fmt.Errorf("unmarshalling response: %+v", err)
return
}
}
}
if err != nil {
Expand Down
36 changes: 19 additions & 17 deletions storage/2023-11-03/blob/blobs/get_page_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,28 @@ func (c Client) GetPageRanges(ctx context.Context, containerName, blobName strin
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Header != nil {
result.ContentType = resp.Header.Get("Content-Type")
result.ETag = resp.Header.Get("ETag")

if v := resp.Header.Get("x-ms-blob-content-length"); v != "" {
i, innerErr := strconv.Atoi(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-blob-content-length` header value %q: %+v", v, innerErr)
return
if err == nil {
if resp.Header != nil {
result.ContentType = resp.Header.Get("Content-Type")
result.ETag = resp.Header.Get("ETag")

if v := resp.Header.Get("x-ms-blob-content-length"); v != "" {
i, innerErr := strconv.Atoi(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-blob-content-length` header value %q: %+v", v, innerErr)
return
}

i64 := int64(i)
result.ContentLength = &i64
}

i64 := int64(i)
result.ContentLength = &i64
}
}

err = resp.Unmarshal(&result)
if err != nil {
err = fmt.Errorf("unmarshalling response: %+v", err)
return
err = resp.Unmarshal(&result)
if err != nil {
err = fmt.Errorf("unmarshalling response: %+v", err)
return
}
}
}
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions storage/2023-11-03/blob/blobs/lease_acquire.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ func (c Client) AcquireLease(ctx context.Context, containerName, blobName string
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Response != nil && resp.Header != nil {
result.LeaseID = resp.Header.Get("x-ms-lease-id")
if err == nil {
if resp.Response != nil && resp.Header != nil {
result.LeaseID = resp.Header.Get("x-ms-lease-id")
}
}
}
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions storage/2023-11-03/blob/blobs/lease_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ func (c Client) ChangeLease(ctx context.Context, containerName, blobName string,
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Response != nil && resp.Header != nil {
result.LeaseID = resp.Header.Get("x-ms-lease-id")
if err == nil {
if resp.Response != nil && resp.Header != nil {
result.LeaseID = resp.Header.Get("x-ms-lease-id")
}
}
}
if err != nil {
Expand Down
112 changes: 57 additions & 55 deletions storage/2023-11-03/blob/blobs/properties_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,68 +200,70 @@ func (c Client) GetProperties(ctx context.Context, containerName, blobName strin
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Header != nil {
result.AccessTier = AccessTier(resp.Header.Get("x-ms-access-tier"))
result.AccessTierChangeTime = resp.Header.Get("x-ms-access-tier-change-time")
result.ArchiveStatus = ArchiveStatus(resp.Header.Get("x-ms-archive-status"))
result.BlobCommittedBlockCount = resp.Header.Get("x-ms-blob-committed-block-count")
result.BlobSequenceNumber = resp.Header.Get("x-ms-blob-sequence-number")
result.BlobType = BlobType(resp.Header.Get("x-ms-blob-type"))
result.CacheControl = resp.Header.Get("Cache-Control")
result.ContentDisposition = resp.Header.Get("Content-Disposition")
result.ContentEncoding = resp.Header.Get("Content-Encoding")
result.ContentLanguage = resp.Header.Get("Content-Language")
result.ContentMD5 = resp.Header.Get("Content-MD5")
result.ContentType = resp.Header.Get("Content-Type")
result.CopyCompletionTime = resp.Header.Get("x-ms-copy-completion-time")
result.CopyDestinationSnapshot = resp.Header.Get("x-ms-copy-destination-snapshot")
result.CopyID = resp.Header.Get("x-ms-copy-id")
result.CopyProgress = resp.Header.Get("x-ms-copy-progress")
result.CopySource = resp.Header.Get("x-ms-copy-source")
result.CopyStatus = CopyStatus(resp.Header.Get("x-ms-copy-status"))
result.CopyStatusDescription = resp.Header.Get("x-ms-copy-status-description")
result.CreationTime = resp.Header.Get("x-ms-creation-time")
result.ETag = resp.Header.Get("Etag")
result.LastModified = resp.Header.Get("Last-Modified")
result.LeaseDuration = LeaseDuration(resp.Header.Get("x-ms-lease-duration"))
result.LeaseState = LeaseState(resp.Header.Get("x-ms-lease-state"))
result.LeaseStatus = LeaseStatus(resp.Header.Get("x-ms-lease-status"))
result.EncryptionScope = resp.Header.Get("x-ms-encryption-scope")
result.MetaData = metadata.ParseFromHeaders(resp.Header)

if v := resp.Header.Get("x-ms-access-tier-inferred"); v != "" {
b, innerErr := strconv.ParseBool(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-access-tier-inferred` header value %q: %s", v, innerErr)
return
if err == nil {
if resp.Header != nil {
result.AccessTier = AccessTier(resp.Header.Get("x-ms-access-tier"))
result.AccessTierChangeTime = resp.Header.Get("x-ms-access-tier-change-time")
result.ArchiveStatus = ArchiveStatus(resp.Header.Get("x-ms-archive-status"))
result.BlobCommittedBlockCount = resp.Header.Get("x-ms-blob-committed-block-count")
result.BlobSequenceNumber = resp.Header.Get("x-ms-blob-sequence-number")
result.BlobType = BlobType(resp.Header.Get("x-ms-blob-type"))
result.CacheControl = resp.Header.Get("Cache-Control")
result.ContentDisposition = resp.Header.Get("Content-Disposition")
result.ContentEncoding = resp.Header.Get("Content-Encoding")
result.ContentLanguage = resp.Header.Get("Content-Language")
result.ContentMD5 = resp.Header.Get("Content-MD5")
result.ContentType = resp.Header.Get("Content-Type")
result.CopyCompletionTime = resp.Header.Get("x-ms-copy-completion-time")
result.CopyDestinationSnapshot = resp.Header.Get("x-ms-copy-destination-snapshot")
result.CopyID = resp.Header.Get("x-ms-copy-id")
result.CopyProgress = resp.Header.Get("x-ms-copy-progress")
result.CopySource = resp.Header.Get("x-ms-copy-source")
result.CopyStatus = CopyStatus(resp.Header.Get("x-ms-copy-status"))
result.CopyStatusDescription = resp.Header.Get("x-ms-copy-status-description")
result.CreationTime = resp.Header.Get("x-ms-creation-time")
result.ETag = resp.Header.Get("Etag")
result.LastModified = resp.Header.Get("Last-Modified")
result.LeaseDuration = LeaseDuration(resp.Header.Get("x-ms-lease-duration"))
result.LeaseState = LeaseState(resp.Header.Get("x-ms-lease-state"))
result.LeaseStatus = LeaseStatus(resp.Header.Get("x-ms-lease-status"))
result.EncryptionScope = resp.Header.Get("x-ms-encryption-scope")
result.MetaData = metadata.ParseFromHeaders(resp.Header)

if v := resp.Header.Get("x-ms-access-tier-inferred"); v != "" {
b, innerErr := strconv.ParseBool(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-access-tier-inferred` header value %q: %s", v, innerErr)
return
}
result.AccessTierInferred = b
}
result.AccessTierInferred = b
}

if v := resp.Header.Get("Content-Length"); v != "" {
i, innerErr := strconv.Atoi(v)
if innerErr != nil {
err = fmt.Errorf("parsing `Content-Length` header value %q: %s", v, innerErr)
if v := resp.Header.Get("Content-Length"); v != "" {
i, innerErr := strconv.Atoi(v)
if innerErr != nil {
err = fmt.Errorf("parsing `Content-Length` header value %q: %s", v, innerErr)
}
result.ContentLength = int64(i)
}
result.ContentLength = int64(i)
}

if v := resp.Header.Get("x-ms-incremental-copy"); v != "" {
b, innerErr := strconv.ParseBool(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-incremental-copy` header value %q: %s", v, innerErr)
return
if v := resp.Header.Get("x-ms-incremental-copy"); v != "" {
b, innerErr := strconv.ParseBool(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-incremental-copy` header value %q: %s", v, innerErr)
return
}
result.IncrementalCopy = b
}
result.IncrementalCopy = b
}

if v := resp.Header.Get("x-ms-server-encrypted"); v != "" {
b, innerErr := strconv.ParseBool(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-server-encrypted` header value %q: %s", v, innerErr)
return
if v := resp.Header.Get("x-ms-server-encrypted"); v != "" {
b, innerErr := strconv.ParseBool(v)
if innerErr != nil {
err = fmt.Errorf("parsing `x-ms-server-encrypted` header value %q: %s", v, innerErr)
return
}
result.ServerEncrypted = b
}
result.ServerEncrypted = b
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions storage/2023-11-03/blob/blobs/put_block_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ func (c Client) PutBlockList(ctx context.Context, containerName, blobName string
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Header != nil {
result.ContentMD5 = resp.Header.Get("Content-MD5")
result.ETag = resp.Header.Get("ETag")
result.LastModified = resp.Header.Get("Last-Modified")
if err == nil {
if resp.Header != nil {
result.ContentMD5 = resp.Header.Get("Content-MD5")
result.ETag = resp.Header.Get("ETag")
result.LastModified = resp.Header.Get("Last-Modified")
}
}
}
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions storage/2023-11-03/blob/blobs/put_block_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ func (c Client) PutBlockFromURL(ctx context.Context, containerName, blobName str
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Header != nil {
result.ContentMD5 = resp.Header.Get("Content-MD5")
if err == nil {
if resp.Header != nil {
result.ContentMD5 = resp.Header.Get("Content-MD5")
}
}
}
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions storage/2023-11-03/blob/blobs/put_page_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ func (c Client) PutPageUpdate(ctx context.Context, containerName, blobName strin
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Header != nil {
result.BlobSequenceNumber = resp.Header.Get("x-ms-blob-sequence-number")
result.ContentMD5 = resp.Header.Get("Content-MD5")
result.LastModified = resp.Header.Get("Last-Modified")
if err == nil {
if resp.Header != nil {
result.BlobSequenceNumber = resp.Header.Get("x-ms-blob-sequence-number")
result.ContentMD5 = resp.Header.Get("Content-MD5")
result.LastModified = resp.Header.Get("Last-Modified")
}
}
}
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions storage/2023-11-03/blob/blobs/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ func (c Client) Snapshot(ctx context.Context, containerName, blobName string, in
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response

if resp.Header != nil {
result.ETag = resp.Header.Get("ETag")
result.SnapshotDateTime = resp.Header.Get("x-ms-snapshot")
if err == nil {
if resp.Header != nil {
result.ETag = resp.Header.Get("ETag")
result.SnapshotDateTime = resp.Header.Get("x-ms-snapshot")
}
}
}
if err != nil {
Expand Down
Loading

0 comments on commit 16f1fbd

Please sign in to comment.