Skip to content

Commit

Permalink
Handling existing permission set errors better
Browse files Browse the repository at this point in the history
  • Loading branch information
fransflippo committed Jul 25, 2021
1 parent edcb498 commit 3cf7e82
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
46 changes: 27 additions & 19 deletions src/helpers/permissionSetHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Connection} from '@salesforce/core';
import {SaveError} from 'jsforce';
import {PermissionSet} from '../types/permissionSet';
import {handleSaveResultError, toApiName} from './sfdx-utils';

Expand All @@ -23,32 +24,39 @@ export class PermissionSetHelper {
const saveResult = await connection.metadata
.create('PermissionSet', permissionSet)
.catch(error => {
if (error.name === 'DUPLICATE_VALUE') {
// Permission set already exists, return false
return false;
} else {
// Synthesize a SaveResult that contains the error information so that all errors are handled in the same way
return {
fullName: permissionSetApiName,
success: false,
errors: {
fields: '',
message: error.toString(),
statusCode: ''
}
};
}
// Synthesize a SaveResult that contains the error information so that all errors are handled in the same way
return {
fullName: permissionSetApiName,
success: false,
errors: {
fields: '',
message: error.toString(),
statusCode: ''
}
};
});
if (saveResult === false) {
return false;
}
if (Array.isArray(saveResult)) {
throw new Error('Expected a single SaveResult but got: ' + saveResult);
}
if (saveResult.success) {
return true;
}
let errors: SaveError[];
if (!Array.isArray(saveResult.errors)) {
errors = [saveResult.errors as SaveError];
} else {
errors = saveResult.errors as SaveError[];
}
for (const i in errors) {
if (errors[i].statusCode === 'DUPLICATE_DEVELOPER_NAME') {
// Duplicate API name: we can safely ignore this
return false;
}
}
// Report error
if (!saveResult.success) {
handleSaveResultError(saveResult);
}
return true;
}

/**
Expand Down
15 changes: 14 additions & 1 deletion test/commands/ci/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,20 @@ the connected app by assigning the permission set using:
connectionIdentityStub.resolves({ user_id: userId, username } as IdentityInfo);
const error = new Error();
error.name = 'DUPLICATE_VALUE';
metadataCreateStub.withArgs('PermissionSet', { fullName: 'Continuous_Integration', label: 'Continuous Integration', description: 'Permission set for the Continuous Integration connected app'}).rejects(error);
metadataCreateStub.withArgs('PermissionSet', { fullName: 'Continuous_Integration', label: 'Continuous Integration', description: 'Permission set for the Continuous Integration connected app'}).resolves({
success: false,
errors: [
{
fields: 'Label',
message: 'The label you entered is in use. Enter a unique label.',
statusCode: 'DUPLICATE_MASTER_LABEL'
}, {
fields: 'Label',
message: 'The API name you entered is already in use. Enter a unique API name.',
statusCode: 'DUPLICATE_DEVELOPER_NAME'
}
]
});
connectionQueryStub.withArgs("SELECT Id, Name from PermissionSet WHERE Name = 'Continuous_Integration'").resolves({ done: true, totalSize: 1, records: [ { Id: 'PermissionSetId' } ]});
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
Expand Down
17 changes: 14 additions & 3 deletions test/helpers/permissionSetHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,20 @@ describe('PermissionSetHelper', () => {
const metadataStub = sinon.createStubInstance<Metadata>(Metadata);
const metadata = metadataStub as unknown as Metadata;
connection.metadata = metadata;
const error = new Error();
error.name = 'DUPLICATE_VALUE';
metadata.create.withArgs('PermissionSet', sinon.match.any).returns(Promise.reject(error));
metadata.create.withArgs('PermissionSet', sinon.match.any).returns(Promise.resolve({
success: false,
errors: [
{
fields: 'Label',
message: 'The label you entered is in use. Enter a unique label.',
statusCode: 'DUPLICATE_MASTER_LABEL'
}, {
fields: 'Label',
message: 'The API name you entered is already in use. Enter a unique API name.',
statusCode: 'DUPLICATE_DEVELOPER_NAME'
}
]
}));

// When
const created = await permissionSetHelper.createPermissionSet(connection, 'My Permissions', 'This is my permission set');
Expand Down

0 comments on commit 3cf7e82

Please sign in to comment.