Skip to content

Commit

Permalink
Catch 412 error in blobsStorage write (#4800)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceciliaavila authored Nov 22, 2024
1 parent 006bcf0 commit 203c044
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
25 changes: 16 additions & 9 deletions libraries/botbuilder-azure-blobs/src/blobsStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,25 @@ export class BlobsStorage implements Storage {

await pmap(
Object.entries(changes),
([key, { eTag = '', ...change }]) => {
const blob = this._containerClient.getBlockBlobClient(sanitizeBlobKey(key));
const serialized = JSON.stringify(change);

return blob.upload(serialized, serialized.length, {
conditions: typeof eTag === 'string' && eTag !== '*' ? { ifMatch: eTag } : {},
blobHTTPHeaders: { blobContentType: 'application/json' },
});
async ([key, { eTag = '', ...change }]) => {
try {
const blob = this._containerClient.getBlockBlobClient(sanitizeBlobKey(key));
const serialized = JSON.stringify(change);
return await blob.upload(serialized, serialized.length, {
conditions: typeof eTag === 'string' && eTag !== '*' ? { ifMatch: eTag } : {},
blobHTTPHeaders: { blobContentType: 'application/json' },
});
} catch (err: any) {
if (err.statusCode === 412) {
throw new Error(`Storage: error writing "${key}" due to eTag conflict.`);
} else {
throw err;
}
}
},
{
concurrency: this._concurrency,
}
},
);
}

Expand Down
15 changes: 15 additions & 0 deletions libraries/botbuilder-azure-blobs/tests/blobsStorage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ describe('BlobsStorage', function () {
maybeIt('should write a set of values', async () => {
await client.write({ foo, bar });
});

maybeIt('should fail with eTag conflict error', async () => {
const changes = {
item1: {
key1: 'value1',
eTag: 'etag1',
},
item2: {
key2: 'value2',
eTag: 'etag1',
},
};

await assert.rejects(() => client.write(changes), 'Storage: error writing "item2" due to eTag conflict.');
});
});

describe('delete()', function () {
Expand Down

0 comments on commit 203c044

Please sign in to comment.