Skip to content

Commit 11eaafe

Browse files
committed
Show a dialog that opens the UI to change the bad setting
1 parent b2fa89f commit 11eaafe

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/configuration.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ import { WorkspaceContext } from "./WorkspaceContext";
1919
import { SwiftToolchain } from "./toolchain/toolchain";
2020
import { showReloadExtensionNotification } from "./ui/ReloadExtension";
2121

22+
/**
23+
* Custom error type for configuration validation errors that includes the setting name
24+
*/
25+
export class ConfigurationValidationError extends Error {
26+
constructor(
27+
public readonly settingName: string,
28+
message: string
29+
) {
30+
super(message);
31+
this.name = "ConfigurationValidationError";
32+
}
33+
}
34+
2235
export type DebugAdapters = "auto" | "lldb-dap" | "CodeLLDB";
2336
export type SetupCodeLLDBOptions =
2437
| "prompt"
@@ -675,22 +688,31 @@ export function substituteVariablesInString(val: string): string {
675688

676689
function validateBooleanSetting(val: boolean, settingName: string): boolean {
677690
if (typeof val !== "boolean") {
678-
throw new Error(`The setting ${settingName} must be a boolean`);
691+
throw new ConfigurationValidationError(
692+
settingName,
693+
`The setting \`${settingName}\` must be a boolean`
694+
);
679695
}
680696
return val;
681697
}
682698

683699
function validateStringSetting<T extends string = string>(val: string, settingName: string): T {
684700
if (typeof val !== "string") {
685-
throw new Error(`The setting ${settingName} must be a string`);
701+
throw new ConfigurationValidationError(
702+
settingName,
703+
`The setting \`${settingName}\` must be a string`
704+
);
686705
}
687706
return val as T;
688707
}
689708

690709
function validateStringArraySettings(arr: string[], settingName: string): string[] {
691710
for (const v of arr) {
692711
if (typeof v !== "string") {
693-
throw new Error(`The setting ${settingName} must be an array of strings`);
712+
throw new ConfigurationValidationError(
713+
settingName,
714+
`The setting \`${settingName}\` must be an array of strings`
715+
);
694716
}
695717
}
696718
return arr;

src/extension.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import { FolderEvent, FolderOperation, WorkspaceContext } from "./WorkspaceConte
2222
import * as commands from "./commands";
2323
import { resolveFolderDependencies } from "./commands/dependencies/resolve";
2424
import { registerSourceKitSchemaWatcher } from "./commands/generateSourcekitConfiguration";
25-
import configuration, { handleConfigurationChangeEvent } from "./configuration";
25+
import configuration, {
26+
ConfigurationValidationError,
27+
handleConfigurationChangeEvent,
28+
} from "./configuration";
2629
import { ContextKeys, createContextKeys } from "./contextKeys";
2730
import { registerDebugger } from "./debugger/debugAdapterFactory";
2831
import * as debug from "./debugger/launch";
@@ -190,10 +193,24 @@ export async function activate(context: vscode.ExtensionContext): Promise<Api> {
190193
},
191194
};
192195
} catch (error) {
193-
const errorMessage = getErrorDescription(error);
194-
// show this error message as the VS Code error message only shows when running
195-
// the extension through the debugger
196-
void vscode.window.showErrorMessage(`Activating Swift extension failed: ${errorMessage}`);
196+
// Handle configuration validation errors with UI that points the user to the poorly configured setting
197+
if (error instanceof ConfigurationValidationError) {
198+
void vscode.window.showErrorMessage(error.message, "Open Settings").then(selection => {
199+
if (selection === "Open Settings") {
200+
void vscode.commands.executeCommand(
201+
"workbench.action.openSettings",
202+
error.settingName
203+
);
204+
}
205+
});
206+
} else {
207+
const errorMessage = getErrorDescription(error);
208+
// show this error message as the VS Code error message only shows when running
209+
// the extension through the debugger
210+
void vscode.window.showErrorMessage(
211+
`Activating Swift extension failed: ${errorMessage}`
212+
);
213+
}
197214
throw error;
198215
}
199216
}

0 commit comments

Comments
 (0)