From 203c044db65f88f0ae7c28de7b379a7f4d17fa51 Mon Sep 17 00:00:00 2001 From: Cecilia Avila <44245136+ceciliaavila@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:38:23 -0300 Subject: [PATCH] Catch 412 error in blobsStorage write (#4800) --- .../src/blobsStorage.ts | 25 ++++++++++++------- .../tests/blobsStorage.test.js | 15 +++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/libraries/botbuilder-azure-blobs/src/blobsStorage.ts b/libraries/botbuilder-azure-blobs/src/blobsStorage.ts index 4b7d02c061..985cd035c8 100644 --- a/libraries/botbuilder-azure-blobs/src/blobsStorage.ts +++ b/libraries/botbuilder-azure-blobs/src/blobsStorage.ts @@ -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, - } + }, ); } diff --git a/libraries/botbuilder-azure-blobs/tests/blobsStorage.test.js b/libraries/botbuilder-azure-blobs/tests/blobsStorage.test.js index 350a4c64a8..348a4e7ce4 100644 --- a/libraries/botbuilder-azure-blobs/tests/blobsStorage.test.js +++ b/libraries/botbuilder-azure-blobs/tests/blobsStorage.test.js @@ -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 () {