Skip to content

Commit 6f29e30

Browse files
committed
Throw, rather than returning null, on getGlobalData() error.
1 parent ac4c324 commit 6f29e30

7 files changed

Lines changed: 54 additions & 40 deletions

File tree

assets/js/googlesitekit/data/utils.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,12 @@ export function createValidatedAction( validate, actionCreator ) {
441441
*
442442
* @param {string} propertyName Property of the global data object.
443443
* @param {string} [childPropertyName] Optional. Property of the object matched by `propertyName`.
444-
* @return {*} The global data object, or the child property value if provided. Returns `null` if the property is not found.
444+
* @return {*} The global data object, or the child property value if provided.
445+
* @throws {Error} If the global data property is not found.
445446
*/
446447
export function getGlobalData( propertyName, childPropertyName = undefined ) {
447448
if ( ! global[ propertyName ] ) {
448-
global.console.error(
449-
`Global data property ${ propertyName } not found.`
450-
);
451-
return null;
449+
throw new Error( `Global data property ${ propertyName } not found.` );
452450
}
453451

454452
if ( childPropertyName ) {

assets/js/googlesitekit/data/utils.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -979,10 +979,8 @@ describe( 'data utils', () => {
979979
describe( 'getGlobalData', () => {
980980
const propertyName = '_googlesitekitTestData';
981981

982-
it( 'should return null when the global data property name is not found', () => {
983-
expect( getGlobalData( 'invalidKey' ) ).toBeNull();
984-
985-
expect( console ).toHaveErroredWith(
982+
it( 'should throw an error when the global data property name is not found', () => {
983+
expect( () => getGlobalData( 'invalidKey' ) ).toThrow(
986984
'Global data property invalidKey not found.'
987985
);
988986
} );

assets/js/googlesitekit/datastore/site/info.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,13 @@ export const resolvers = {
286286
return;
287287
}
288288

289-
const baseData = getGlobalData( '_googlesitekitBaseData' );
290-
const entityData = getGlobalData( '_googlesitekitEntityData' );
289+
let baseData;
290+
let entityData;
291291

292-
if ( baseData === null || entityData === null ) {
292+
try {
293+
baseData = getGlobalData( '_googlesitekitBaseData' );
294+
entityData = getGlobalData( '_googlesitekitEntityData' );
295+
} catch ( error ) {
293296
global.console.error( 'Could not load core/site info.' );
294297
return;
295298
}

assets/js/googlesitekit/datastore/user/permissions.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,17 @@ const baseResolvers = {
164164
// has already expired.
165165
// We'll still fetch it in case something has changed,
166166
// but receive the preloaded right away.
167-
const preloadedPermissions = getGlobalData(
168-
'_googlesitekitAPIFetchData',
169-
'preloadedData'
170-
)?.[ '/google-site-kit/v1/core/user/data/permissions' ]?.body;
167+
let preloadedPermissions;
168+
169+
try {
170+
preloadedPermissions = getGlobalData(
171+
'_googlesitekitAPIFetchData',
172+
'preloadedData'
173+
)?.[ '/google-site-kit/v1/core/user/data/permissions' ]?.body;
174+
} catch ( error ) {
175+
global.console.error( 'Could not load core/user permissions.' );
176+
return;
177+
}
171178

172179
if ( preloadedPermissions ) {
173180
yield fetchGetCapabilitiesStore.actions.receiveGetCapabilities( {

assets/js/googlesitekit/datastore/user/user-info.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,10 @@ export const resolvers = {
208208
return;
209209
}
210210

211-
const userData = getGlobalData( '_googlesitekitUserData' );
212-
213-
if ( userData === null ) {
211+
let userData;
212+
try {
213+
userData = getGlobalData( '_googlesitekitUserData' );
214+
} catch ( error ) {
214215
global.console.error( 'Could not load core/user info.' );
215216
return;
216217
}

assets/js/googlesitekit/modules/datastore/modules.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -834,11 +834,12 @@ const baseResolvers = {
834834
return;
835835
}
836836

837-
const dashboardSharingData = getGlobalData(
838-
'_googlesitekitDashboardSharingData'
839-
);
840-
841-
if ( dashboardSharingData === null ) {
837+
let dashboardSharingData;
838+
try {
839+
dashboardSharingData = getGlobalData(
840+
'_googlesitekitDashboardSharingData'
841+
);
842+
} catch ( error ) {
842843
global.console.error(
843844
'Could not load core/modules dashboard sharing.'
844845
);
@@ -857,8 +858,10 @@ const baseResolvers = {
857858
return;
858859
}
859860

860-
const inlineModulesData = getGlobalData( '_googlesitekitModulesData' );
861-
if ( inlineModulesData === null ) {
861+
let inlineModulesData;
862+
try {
863+
inlineModulesData = getGlobalData( '_googlesitekitModulesData' );
864+
} catch ( error ) {
862865
return;
863866
}
864867

assets/js/googlesitekit/modules/datastore/sharing-settings.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,12 @@ const baseResolvers = {
410410
return;
411411
}
412412

413-
const dashboardSharingData = getGlobalData(
414-
'_googlesitekitDashboardSharingData'
415-
);
416-
417-
if ( dashboardSharingData === null ) {
413+
let dashboardSharingData;
414+
try {
415+
dashboardSharingData = getGlobalData(
416+
'_googlesitekitDashboardSharingData'
417+
);
418+
} catch ( error ) {
418419
global.console.error(
419420
'Could not load core/modules dashboard sharing settings.'
420421
);
@@ -433,11 +434,12 @@ const baseResolvers = {
433434
return;
434435
}
435436

436-
const dashboardSharingData = getGlobalData(
437-
'_googlesitekitDashboardSharingData'
438-
);
439-
440-
if ( dashboardSharingData === null ) {
437+
let dashboardSharingData;
438+
try {
439+
dashboardSharingData = getGlobalData(
440+
'_googlesitekitDashboardSharingData'
441+
);
442+
} catch ( error ) {
441443
global.console.error(
442444
'Could not load core/modules dashboard sharing roles.'
443445
);
@@ -458,10 +460,12 @@ const baseResolvers = {
458460
return;
459461
}
460462

461-
const dashboardSharingData = getGlobalData(
462-
'_googlesitekitDashboardSharingData'
463-
);
464-
if ( dashboardSharingData === null ) {
463+
let dashboardSharingData;
464+
try {
465+
dashboardSharingData = getGlobalData(
466+
'_googlesitekitDashboardSharingData'
467+
);
468+
} catch ( error ) {
465469
global.console.error(
466470
'Could not load core/modules dashboard sharing.'
467471
);

0 commit comments

Comments
 (0)