Skip to content

Commit

Permalink
Merge pull request #61 from dbreese/handle-sobject-dne
Browse files Browse the repository at this point in the history
Handle case where an error occurs of any type when attempting to get page layout for missing objects.
  • Loading branch information
dbreese authored Feb 20, 2024
2 parents 0464867 + 911baf8 commit bc1c271
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 94 deletions.
13 changes: 13 additions & 0 deletions resources/instructions/createSObjectLwcQuickActions.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ <h1>Create sObject LWC Quick Actions</h1>
</tbody>
</table>

<div id="errorDiv" style="color: crimson">
<!-- placeholder for any error messages -->
</div>

<label
style="display: block; margin-top: 10px; margin-bottom: 8px"
for="generateLwcQuickActionsButton"
Expand All @@ -81,6 +85,8 @@ <h1>Create sObject LWC Quick Actions</h1>
'quickActionStatusTable'
);

const errorDiv = document.getElementById('errorDiv');

generateQuickActionsButtonElement.addEventListener('click', () => {
webviewMessaging.sendMessageRequest(
'generateLwcQuickActions',
Expand All @@ -105,6 +111,13 @@ <h1>Create sObject LWC Quick Actions</h1>
});

function handleQuickActionStatusResponse(response) {
// check for an error
errorDiv.innerHTML = '';
if (response.error != undefined) {
errorDiv.innerHTML = 'An error occurred: ' + response.error;
return;
}

var tbody =
quickActionStatusTable.getElementsByTagName('tbody')[0];

Expand Down
193 changes: 109 additions & 84 deletions src/commands/wizard/lwcGenerationCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ export type QuickActionStatus = {
};

export type SObjectQuickActionStatus = {
error?: string;
sobjects: {
[name: string]: QuickActionStatus;
};
};

export type GetSObjectsStatus = {
error?: string;
sobjects: string[];
};

Expand All @@ -54,17 +52,29 @@ export class LwcGenerationCommand {
type: 'generateLwcQuickActions',
action: async (_panel, _data, callback) => {
const quickActionStatus =
await LwcGenerationCommand.checkForExistingQuickActions();
await LwcGenerationCommand.checkForExistingQuickActions().catch(
(error) => {
if (callback) {
callback({ error: error });
}
return;
}
);

const newLwcQuickActionStatus =
await LwcGenerationCommand.generateMissingLwcsAndQuickActions(
extensionUri,
quickActionStatus
);
quickActionStatus!
).catch((error) => {
if (callback) {
callback({ error: error });
}
return;
});

// send back updates so UI can be refreshed
if (callback) {
callback(newLwcQuickActionStatus);
callback(newLwcQuickActionStatus!);
}
}
},
Expand All @@ -73,8 +83,13 @@ export class LwcGenerationCommand {
action: async (_panel, _data, callback) => {
if (callback) {
const quickActionStatus =
await LwcGenerationCommand.checkForExistingQuickActions();
callback(quickActionStatus);
await LwcGenerationCommand.checkForExistingQuickActions().catch(
(error) => {
callback({ error: error });
return;
}
);
callback(quickActionStatus!);
}
}
}
Expand All @@ -84,7 +99,7 @@ export class LwcGenerationCommand {
}

static async getSObjectsFromLandingPage(): Promise<GetSObjectsStatus> {
return new Promise<GetSObjectsStatus>(async (resolve) => {
return new Promise<GetSObjectsStatus>(async (resolve, reject) => {
const staticResourcesPath =
await WorkspaceUtils.getStaticResourcesDir();
const landingPageJson = 'landing_page.json';
Expand All @@ -101,107 +116,117 @@ export class LwcGenerationCommand {
await access(landingPagePath);
const uem = CommonUtils.loadJsonFromFile(landingPagePath);
getSObjectsStatus.sobjects = UEMParser.findSObjects(uem);
resolve(getSObjectsStatus);
} catch (err) {
console.warn(
`File '${landingPageJson}' does not exist at '${staticResourcesPath}'.`
);
getSObjectsStatus.error = (err as Error).message;
reject((err as Error).message);
}

resolve(getSObjectsStatus);
});
}

static async checkForExistingQuickActions(): Promise<SObjectQuickActionStatus> {
return new Promise<SObjectQuickActionStatus>(async (resolve) => {
const results: SObjectQuickActionStatus = { sobjects: {} };
return new Promise<SObjectQuickActionStatus>(
async (resolve, reject) => {
const results: SObjectQuickActionStatus = { sobjects: {} };

const sObjectsStatus =
await this.getSObjectsFromLandingPage().catch((error) => {
return reject(error);
});

sObjectsStatus!.sobjects.forEach((sobject) => {
const quickActionStatus: QuickActionStatus = {
view: false,
edit: false,
create: false
};
quickActionStatus.view =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'view'
);
quickActionStatus.edit =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'edit'
);
quickActionStatus.create =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'create'
);

results.sobjects[sobject] = quickActionStatus;
});

const sObjectsStatus = await this.getSObjectsFromLandingPage();
if (sObjectsStatus.error) {
results.error = sObjectsStatus.error;
return resolve(results);
}

sObjectsStatus.sobjects.forEach((sobject) => {
const quickActionStatus: QuickActionStatus = {
view: false,
edit: false,
create: false
};
quickActionStatus.view =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'view'
);
quickActionStatus.edit =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'edit'
);
quickActionStatus.create =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'create'
);

results.sobjects[sobject] = quickActionStatus;
});

return resolve(results);
});
);
}

static async generateMissingLwcsAndQuickActions(
extensionUri: Uri,
quickActionStatus: SObjectQuickActionStatus
): Promise<SObjectQuickActionStatus> {
return new Promise<SObjectQuickActionStatus>(async (resolve) => {
for (const sobject in quickActionStatus.sobjects) {
try {
const quickActions = quickActionStatus.sobjects[sobject];

if (
!quickActions.create ||
!quickActions.edit ||
!quickActions.view
) {
// at least 1 needs to be created
const compactLayoutFields =
await OrgUtils.getCompactLayoutFieldsForSObject(
sobject
return new Promise<SObjectQuickActionStatus>(
async (resolve, reject) => {
for (const sobject in quickActionStatus.sobjects) {
try {
const quickActions =
quickActionStatus.sobjects[sobject];

if (
!quickActions.create ||
!quickActions.edit ||
!quickActions.view
) {
// at least 1 needs to be created
const compactLayoutFields =
await OrgUtils.getCompactLayoutFieldsForSObject(
sobject
).catch((err) => {
reject(
`An error occurred while obtaining layout for ${sobject} : ${
(err as Error).message
}`
);
return;
});

const codeBuilder = new CodeBuilder(
extensionUri,
sobject,
compactLayoutFields!
);

const codeBuilder = new CodeBuilder(
extensionUri,
sobject,
compactLayoutFields
);

if (!quickActions.view) {
await codeBuilder.generateView();
}
if (!quickActions.view) {
await codeBuilder.generateView();
}

if (!quickActions.edit) {
await codeBuilder.generateEdit();
}
if (!quickActions.edit) {
await codeBuilder.generateEdit();
}

if (!quickActions.create) {
await codeBuilder.generateCreate();
if (!quickActions.create) {
await codeBuilder.generateCreate();
}
}
} catch (err) {
console.error(
`Could not generate quick actions for sobject ${sobject}, so skipping`,
err
);
}
} catch (err) {
console.error(
`Could not generate quick actions for sobject ${sobject}, so skipping`,
err
);
}
}

// Just double check now that things have been created.
const newStatus =
await LwcGenerationCommand.checkForExistingQuickActions();
resolve(newStatus);
});
// Just double check now that things have been created.
const newStatus =
await LwcGenerationCommand.checkForExistingQuickActions();
resolve(newStatus);
}
);
}

private static checkForExistingQuickAction(
Expand Down
Loading

0 comments on commit bc1c271

Please sign in to comment.