From ff0e918448351c9bf63fd97cfa025483df4249b2 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 5 Jun 2024 12:02:36 +0400 Subject: [PATCH 1/3] Data: Add error handle to the 'registry.batch' method --- packages/data/src/registry.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/data/src/registry.js b/packages/data/src/registry.js index 02a0b19136a3e0..1a33bc4b7c7b07 100644 --- a/packages/data/src/registry.js +++ b/packages/data/src/registry.js @@ -322,9 +322,21 @@ export function createRegistry( storeConfigs = {}, parent = null ) { emitter.pause(); Object.values( stores ).forEach( ( store ) => store.emitter.pause() ); - callback(); - emitter.resume(); - Object.values( stores ).forEach( ( store ) => store.emitter.resume() ); + try { + callback(); + emitter.resume(); + Object.values( stores ).forEach( ( store ) => + store.emitter.resume() + ); + } catch ( error ) { + emitter.resume(); + Object.values( stores ).forEach( ( store ) => + store.emitter.resume() + ); + + // Re-throw the error after resuming the work for debugging. + throw error; + } } let registry = { From 8652c01e14c14708a04967f25822da9d656c7b86 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 5 Jun 2024 13:04:45 +0400 Subject: [PATCH 2/3] Use 'try...finaly' --- packages/data/src/registry.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/data/src/registry.js b/packages/data/src/registry.js index 1a33bc4b7c7b07..3e7a8fdd8b5a07 100644 --- a/packages/data/src/registry.js +++ b/packages/data/src/registry.js @@ -324,18 +324,11 @@ export function createRegistry( storeConfigs = {}, parent = null ) { Object.values( stores ).forEach( ( store ) => store.emitter.pause() ); try { callback(); + } finally { emitter.resume(); Object.values( stores ).forEach( ( store ) => store.emitter.resume() ); - } catch ( error ) { - emitter.resume(); - Object.values( stores ).forEach( ( store ) => - store.emitter.resume() - ); - - // Re-throw the error after resuming the work for debugging. - throw error; } } From 98b9db91e531b03be0516d044a053d4abe42c682 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 5 Jun 2024 16:48:04 +0400 Subject: [PATCH 3/3] Add unit test --- packages/data/src/test/registry.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/data/src/test/registry.js b/packages/data/src/test/registry.js index df9cb774dfc8cf..46b914d776f132 100644 --- a/packages/data/src/test/registry.js +++ b/packages/data/src/test/registry.js @@ -755,6 +755,28 @@ describe( 'createRegistry', () => { } ); expect( listener ).toHaveBeenCalledTimes( 1 ); } ); + + it( 'should handle errors', () => { + const store = registry.registerStore( 'myAwesomeReducer', { + reducer: ( state = 0 ) => state + 1, + } ); + const listener = jest.fn(); + const error = new Error( 'Whoops' ); + subscribeWithUnsubscribe( listener ); + + expect( () => { + registry.batch( () => { + throw error; + } ); + } ).toThrow( error ); + expect( listener ).not.toHaveBeenCalled(); + + registry.batch( () => { + store.dispatch( { type: 'dummy' } ); + store.dispatch( { type: 'dummy' } ); + } ); + expect( listener ).toHaveBeenCalledTimes( 1 ); + } ); } ); describe( 'use', () => {