Skip to content

Commit

Permalink
Merge pull request #9546 from google/bug/#9471-capture-insufficient-e…
Browse files Browse the repository at this point in the history
…rror-create-audience-api

Bug/#9471 - Capture insufficient permissions error for `create-audience` request failures
  • Loading branch information
techanvil authored Oct 23, 2024
2 parents 3ac038e + 47ad626 commit fbcf92f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
13 changes: 12 additions & 1 deletion assets/js/modules/analytics-4/datastore/audiences.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { createFetchStore } from '../../../googlesitekit/data/create-fetch-store
import { createValidatedAction } from '../../../googlesitekit/data/utils';
import { CORE_USER } from '../../../googlesitekit/datastore/user/constants';
import { getPreviousDate } from '../../../util';
import { isInsufficientPermissionsError } from '../../../util/errors';
import { validateAudience } from '../utils/validation';
import { RESOURCE_TYPE_AUDIENCE } from './partial-data';

Expand Down Expand Up @@ -429,16 +430,26 @@ const baseActions = {
);

const failedAudiencesToRetry = [];
let insufficientPermissionsError = null;

audienceCreationResults.forEach( ( result, index ) => {
const audienceSlug = audiencesToCreate[ index ];

if ( result.error ) {
failedAudiencesToRetry.push( audienceSlug );
if ( isInsufficientPermissionsError( result.error ) ) {
insufficientPermissionsError = result.error;
} else {
failedAudiencesToRetry.push( audienceSlug );
}
} else {
configuredAudiences.push( result.response.name );
}
} );

if ( insufficientPermissionsError ) {
return { error: insufficientPermissionsError };
}

yield commonActions.await(
resolveSelect( CORE_USER ).getAudienceSettings()
);
Expand Down
42 changes: 42 additions & 0 deletions assets/js/modules/analytics-4/datastore/audiences.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
SITE_KIT_AUDIENCE_DEFINITIONS,
} from './constants';
import { CORE_USER } from '../../../googlesitekit/datastore/user/constants';
import { ERROR_REASON_INSUFFICIENT_PERMISSIONS } from '../../../util/errors';
import {
properties as propertiesFixture,
audiences as audiencesFixture,
Expand Down Expand Up @@ -1573,6 +1574,47 @@ describe( 'modules/analytics-4 audiences', () => {
registry.select( CORE_USER ).getConfiguredAudiences()
).toEqual( expectedConfiguredAudiences );
} );

it( 'should return an insufficient permisions error if the "create-audience" request fails', async () => {
const insufficientPermissionsError = {
code: 'test_error',
message: 'Error message.',
data: {
reason: ERROR_REASON_INSUFFICIENT_PERMISSIONS,
},
};

fetchMock.postOnce( syncAvailableAudiencesEndpoint, {
body: [],
status: 200,
} );

// Mocking createAudience API call with insufficient permissions error.
fetchMock.post(
{ url: createAudienceEndpoint, repeat: 2 },
{
body: insufficientPermissionsError,
status: 400,
}
);

const { response, error } = await registry
.dispatch( MODULES_ANALYTICS_4 )
.enableAudienceGroup();

await waitForDefaultTimeouts();

expect( response ).toBeUndefined();
expect( error ).toEqual( insufficientPermissionsError );

// Ensuring no configured audiences are set when all creation attempts fail.
expect(
registry.select( CORE_USER ).getConfiguredAudiences()
).toBeNull();

expect( console ).toHaveErrored();
expect( console ).toHaveErrored();
} );
} );
} );

Expand Down

0 comments on commit fbcf92f

Please sign in to comment.