Skip to content

Commit

Permalink
wrap commands in try/catch blocks for registering
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel OBrien committed Feb 2, 2024
1 parent fb2135b commit 0621f92
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "launchdarkly",
"displayName": "LaunchDarkly",
"description": "Manage LaunchDarkly feature flags directly from within your code",
"version": "4.99.51",
"version": "4.99.52",
"publisher": "launchdarklyofficial",
"engines": {
"vscode": "^1.82.0"
Expand Down
5 changes: 3 additions & 2 deletions src/commands/configureLaunchDarkly.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { commands, Disposable, ProgressLocation, window } from 'vscode';
import { Disposable, ProgressLocation, window } from 'vscode';
import { ConfigurationMenu } from '../configurationMenu';
import { FlagStore } from '../flagStore';
import { LDExtensionConfiguration } from '../ldExtensionConfiguration';
import { registerCommand } from '../utils';

export default function configureLaunchDarkly(config: LDExtensionConfiguration) {
const configureExtension: Disposable = commands.registerCommand('extension.configureLaunchDarkly', async () => {
const configureExtension: Disposable = registerCommand('extension.configureLaunchDarkly', async () => {
try {
const configurationMenu = new ConfigurationMenu(config);
await configurationMenu.configure();
Expand Down
5 changes: 3 additions & 2 deletions src/commands/createFlag.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { commands, Disposable } from 'vscode';
import { Disposable } from 'vscode';
import { CreateFlagMenu } from '../createFlagMenu';
import { LDExtensionConfiguration } from '../ldExtensionConfiguration';
import { registerCommand } from '../utils';

export default function createFlagCmd(config: LDExtensionConfiguration): Disposable {
const createFlagCmd = commands.registerCommand('launchdarkly.createFlag', async () => {
const createFlagCmd = registerCommand('launchdarkly.createFlag', async () => {
const configurationMenu = new CreateFlagMenu(config);
await configurationMenu.collectInputs();
});
Expand Down
5 changes: 3 additions & 2 deletions src/commands/enableCodeLens.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { commands, Disposable, workspace } from 'vscode';
import { Disposable, workspace } from 'vscode';
import { LDExtensionConfiguration } from '../ldExtensionConfiguration';
import { registerCommand } from '../utils';

export default function enableCodeLensConfig(config: LDExtensionConfiguration): Disposable {
const enableCodeLens: Disposable = commands.registerCommand('launchdarkly.enableCodeLens', async () => {
const enableCodeLens: Disposable = registerCommand('launchdarkly.enableCodeLens', async () => {
workspace.getConfiguration('launchdarkly').update('enableCodeLens', !config.getConfig().enableCodeLens);
config.getConfig().enableCodeLens = !config.getConfig().enableCodeLens;
});
Expand Down
3 changes: 2 additions & 1 deletion src/commands/evaluateFlag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import os from 'os';
import { LDExtensionConfiguration } from '../ldExtensionConfiguration';
import { LDMultiKindContext, LDSingleKindContext } from '@launchdarkly/node-server-sdk';
import { YamlContextReader } from '../utils/contextYAML';
import { registerCommand } from '../utils';

// Not officially implemented. Leaving for future decision.

Expand All @@ -21,7 +22,7 @@ type flagSelection = {
};

export default function flagEvalCmd(config: LDExtensionConfiguration): Disposable {
const flagEvalCmd = commands.registerCommand('launchdarkly.quickEval', async () => {
const flagEvalCmd = registerCommand('launchdarkly.quickEval', async () => {
let flags;
try {
flags = await config.getFlagStore()?.allFlagsMetadata();
Expand Down
4 changes: 2 additions & 2 deletions src/commands/flagActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { QuickPickItemKind, Disposable, commands, window } from 'vscode';
import { LDExtensionConfiguration } from '../ldExtensionConfiguration';
import { FlagQuickPickItem, targetFlag } from './selectRule';
import { ToggleCache } from '../toggleCache';
import { flagCodeSearch, flagOffFallthroughPatch, toggleFlag } from '../utils';
import { flagCodeSearch, flagOffFallthroughPatch, registerCommand, toggleFlag } from '../utils';

const cache = new ToggleCache();

export default function flagCmd(config: LDExtensionConfiguration): Disposable {
const flagCmd = commands.registerCommand('launchdarkly.quickFlag', async () => {
const flagCmd = registerCommand('launchdarkly.quickFlag', async () => {
const flags = await config.getFlagStore()?.allFlagsMetadata();
if (flags === undefined) {
// Errors would be handled in the flagStore
Expand Down
8 changes: 4 additions & 4 deletions src/commands/selectRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LDExtensionConfiguration } from '../ldExtensionConfiguration';
import { Dictionary, isArray } from 'lodash';
import crypto from 'crypto';
import { Clause, FeatureFlag } from '../models';
import { logDebugMessage } from '../utils';
import { logDebugMessage, registerCommand } from '../utils';

const cache = new ToggleCache();
const revertLastCmd = {};
Expand All @@ -24,7 +24,7 @@ export interface FlagQuickPickItem extends QuickPickItem {
}

export default function selectRuleCmd(config: LDExtensionConfiguration): Disposable {
const selectRuleCmd = commands.registerCommand('launchdarkly.quickPickRules', async () => {
const selectRuleCmd = registerCommand('launchdarkly.quickPickRules', async () => {
const flags = await config.getFlagStore()?.allFlagsMetadata();
if (flags === undefined) {
// Errors would be handled in the flagStore
Expand Down Expand Up @@ -369,8 +369,8 @@ export async function targetFlag(
window.showInformationMessage('No rules found in rules.yaml');
return;
}
const ruleNames = mapObjects(rules['rules'], 'name', 'rule');
const targetNames = mapObjects(rules['targets'], 'name', 'target');
const ruleNames = rules['rules'] ? mapObjects(rules['rules'], 'name', 'rule') : [];
const targetNames = rules['targets'] ? mapObjects(rules['targets'], 'name', 'target') : [];
const targetDivider = {
label: 'Individual Targeting',
kind: QuickPickItemKind.Separator,
Expand Down
5 changes: 3 additions & 2 deletions src/commands/setGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ExtensionContext, commands, window } from 'vscode';
import { ExtensionContext, window } from 'vscode';
import { registerCommand } from '../utils';

export function SetGlobalCmd(ctx: ExtensionContext) {
const proj = ctx.workspaceState.get('project');
const env = ctx.workspaceState.get('env');
const disposable = commands.registerCommand('launchdarkly.setGlobalDefaults', () => {
const disposable = registerCommand('launchdarkly.setGlobalDefaults', () => {
// The code you want to run when the command is executed
window
.showInformationMessage(
Expand Down
3 changes: 2 additions & 1 deletion src/commands/setMaintainer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { commands, Disposable, window } from 'vscode';
import { LDExtensionConfiguration } from '../ldExtensionConfiguration';
import { registerCommand } from '../utils';

// This function is only called from a location that already checks if a team is available.
// If that changes more logic needs to be moved here
export default function setMaintainerCmd(config: LDExtensionConfiguration): Disposable {
const setMaintainerCmd = commands.registerCommand('launchdarkly.setMaintainer', async (args) => {
const setMaintainerCmd = registerCommand('launchdarkly.setMaintainer', async (args) => {
try {
const key = args;
if (key) {
Expand Down
5 changes: 2 additions & 3 deletions src/commands/setWorkspaceEnabled.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { commands } from 'vscode';
import { LDExtensionConfiguration } from '../ldExtensionConfiguration';
import { extensionReload } from '../utils';
import { extensionReload, registerCommand } from '../utils';

export function SetWorkspaceCmd(config: LDExtensionConfiguration) {
const disposable = commands.registerCommand('launchdarkly.enableWorkspace', async () => {
const disposable = registerCommand('launchdarkly.enableWorkspace', async () => {
// The code you want to run when the command is executed
if (
config.getCtx().workspaceState.get('isDisabledForWorkspace') !== false ||
Expand Down
5 changes: 3 additions & 2 deletions src/commands/toggleFlagContext.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { commands, Disposable, window } from 'vscode';
import { Disposable, window } from 'vscode';
import { LDExtensionConfiguration } from '../ldExtensionConfiguration';
import { registerCommand } from '../utils';

export default function toggleFlagCtxCmd(config: LDExtensionConfiguration): Disposable {
const toggleFlagCtxCmd = commands.registerCommand('launchdarkly.toggleFlagContext', async (args) => {
const toggleFlagCtxCmd = registerCommand('launchdarkly.toggleFlagContext', async (args) => {
try {
const key = args ? args : (config.getCtx().workspaceState.get('LDFlagKey') as string);

Expand Down
18 changes: 9 additions & 9 deletions src/providers/flagsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { authentication } from 'vscode';
import { FlagNode, FlagParentNode, flagToValues } from '../utils/FlagNode';
import { generateHoverString } from '../utils/hover';
import { LDExtensionConfiguration } from '../ldExtensionConfiguration';
import { flagCodeSearch, logDebugMessage } from '../utils';
import { flagCodeSearch, logDebugMessage, registerCommand } from '../utils';
import { ReleaseFlagNode } from './releaseViewProvider';

const COLLAPSED = vscode.TreeItemCollapsibleState.Collapsed;
Expand Down Expand Up @@ -204,7 +204,7 @@ export class LaunchDarklyTreeViewProvider implements vscode.TreeDataProvider<Fla
}

registerTreeviewRefreshCommand(): vscode.Disposable {
return vscode.commands.registerCommand('launchdarkly.treeviewrefresh', (): void => {
return registerCommand('launchdarkly.treeviewrefresh', (): void => {
this.reload();
vscode.commands.executeCommand(
'setContext',
Expand All @@ -221,23 +221,23 @@ export class LaunchDarklyTreeViewProvider implements vscode.TreeDataProvider<Fla
return;
}
this.ldConfig.getCtx().subscriptions.push(
vscode.commands.registerCommand(copyKeyCmd, (node: FlagNode) => vscode.env.clipboard.writeText(node.flagKey)),
vscode.commands.registerCommand('launchdarkly.openBrowser', (node: FlagNode | string) => {
registerCommand(copyKeyCmd, (node: FlagNode) => vscode.env.clipboard.writeText(node.flagKey)),
registerCommand('launchdarkly.openBrowser', (node: FlagNode | string) => {
if (typeof node === 'string') {
vscode.env.openExternal(vscode.Uri.parse(node));
} else if (node.uri) {
vscode.env.openExternal(vscode.Uri.parse(node.uri));
}
}),
vscode.commands.registerCommand('launchdarkly.refreshEntry', () => this.reload()),
registerCommand('launchdarkly.refreshEntry', () => this.reload()),
this.registerTreeviewRefreshCommand(),
vscode.commands.registerCommand('launchdarkly.flagMultipleSearch', (node: FlagNode | ReleaseFlagNode) => {
registerCommand('launchdarkly.flagMultipleSearch', (node: FlagNode | ReleaseFlagNode) => {
if (!node.flagKey) {
return;
}
flagCodeSearch(this.ldConfig, node.flagKey);
}),
vscode.commands.registerCommand('launchdarkly.toggleFlag', async (node: FlagParentNode) => {
registerCommand('launchdarkly.toggleFlag', async (node: FlagParentNode) => {
try {
if (!node.flagKey) {
logDebugMessage('Flag key not found');
Expand All @@ -254,7 +254,7 @@ export class LaunchDarklyTreeViewProvider implements vscode.TreeDataProvider<Fla
vscode.window.showErrorMessage(`Could not toggle flag: ${err.message}`);
}
}),
vscode.commands.registerCommand('launchdarkly.fallthroughChange', async (node: FlagNode) => {
registerCommand('launchdarkly.fallthroughChange', async (node: FlagNode) => {
try {
await this.flagPatch(
node,
Expand All @@ -265,7 +265,7 @@ export class LaunchDarklyTreeViewProvider implements vscode.TreeDataProvider<Fla
vscode.window.showErrorMessage(`Could not set Fallthrough: ${err.message}`);
}
}),
vscode.commands.registerCommand('launchdarkly.offChange', async (node: FlagNode) => {
registerCommand('launchdarkly.offChange', async (node: FlagNode) => {
try {
await this.flagPatch(node, `/environments/${this.ldConfig.getConfig()?.env}/offVariation`, node.contextValue);
} catch (err) {
Expand Down
3 changes: 2 additions & 1 deletion src/providers/quickLinksView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from 'vscode';
import checkExistingCommand from '../utils/common';
import { LDExtensionConfiguration } from '../ldExtensionConfiguration';
import { registerCommand } from '../utils';

const NON_COLLAPSED = TreeItemCollapsibleState.None;

Expand All @@ -37,7 +38,7 @@ export class QuickLinksListProvider implements TreeDataProvider<TreeItem> {
if (await checkExistingCommand(compareFlagsCmd)) {
return;
}
commands.registerCommand(compareFlagsCmd, async () => {
registerCommand(compareFlagsCmd, async () => {
let values: QuickPickItem[] = [{ label: 'No flags found', description: '' }];
if (this.config.getFlagStore() !== null) {
const flags = await this.config.getFlagStore().allFlagsMetadata();
Expand Down
22 changes: 15 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function setupComponents(config: LDExtensionConfiguration, reload =
const listView = new LaunchDarklyFlagListProvider(config, codeLens);
window.registerTreeDataProvider('launchdarklyFlagList', listView);
if (!reload) {
listViewDisp = commands.registerCommand('launchdarkly.refreshFlagLens', () => listView.setFlagsinDocument());
listViewDisp = registerCommand('launchdarkly.refreshFlagLens', () => listView.setFlagsinDocument());
config.getCtx().subscriptions.push(listViewDisp);
}

Expand Down Expand Up @@ -119,11 +119,10 @@ export async function setupComponents(config: LDExtensionConfiguration, reload =

codeLens.start();

if (!reload) {
const flagToggle = commands.registerCommand('launchdarkly.toggleFlagCmdPrompt', async () => {
const flagToggle = registerCommand('launchdarkly.toggleFlagCmdPrompt', async () => {
await showToggleMenu(config);
});
const openFlag = commands.registerCommand('launchdarkly.OpenFlag', (node: FlagItem) =>
const openFlag = registerCommand('launchdarkly.OpenFlag', (node: FlagItem) =>
window.activeTextEditor.revealRange(node.range),
);

Expand All @@ -139,10 +138,9 @@ export async function setupComponents(config: LDExtensionConfiguration, reload =
);
await config.getCtx().globalState.update('commands', allDisposables);
config.getCtx().subscriptions.push(flagToggle, openFlag);
} catch(err) {
logDebugMessage(err);
}
} catch (err) {
console.error(err);
}
}

async function showToggleMenu(config: LDExtensionConfiguration) {
Expand Down Expand Up @@ -310,3 +308,13 @@ export function legacyAuth() {
return true;
//workspace.getConfiguration('launchdarkly').get('legacyAuth', false)
}


export function registerCommand(command: string, callback: (...args: any[]) => any) {
try {
return commands.registerCommand(command, callback);
} catch (err) {
logDebugMessage(err);
return Disposable.from();
}
}
5 changes: 3 additions & 2 deletions src/utils/checkCoderefs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from 'path';
import { ExtensionContext, FileType, Uri, commands, window, workspace } from 'vscode';
import { ExtensionContext, FileType, Uri, window, workspace } from 'vscode';
import { registerCommand } from '../utils';

export function checkCodeRefs(ctx: ExtensionContext) {
const disposable = commands.registerCommand('extension.checkLaunchDarkly', async () => {
const disposable = registerCommand('extension.checkLaunchDarkly', async () => {
const workspaceFolders = workspace.workspaceFolders;

if (workspaceFolders) {
Expand Down
41 changes: 22 additions & 19 deletions src/utils/rulesYaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,28 @@ const schema = {
type: 'string',
},
clauses: {
type: 'object',
properties: {
attribute: {
type: 'string',
},
op: {
type: 'string',
},
values: {
type: 'array',
},
negate: {
type: 'boolean',
},
contextKind: {
type: 'string',
},
_key: {
type: 'string',
type: 'array',
items: {
type: 'object',
properties: {
attribute: {
type: 'string',
},
op: {
type: 'string',
},
values: {
type: 'array',
},
negate: {
type: 'boolean',
},
contextKind: {
type: 'string',
},
_key: {
type: 'string',
},
},
},
},
Expand Down

0 comments on commit 0621f92

Please sign in to comment.