diff --git a/src/commands/show.ts b/src/commands/show.ts index e4553973..79c0fbbf 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -65,10 +65,10 @@ async function fetchProblemLanguage(): Promise { if (defaultLanguage && languages.indexOf(defaultLanguage) < 0) { defaultLanguage = undefined; } - const language: string | undefined = defaultLanguage || await vscode.window.showQuickPick(languages, { placeHolder: "Select the language you want to use" }); + const language: string | undefined = defaultLanguage || await vscode.window.showQuickPick(languages, { placeHolder: "Select the language you want to use", ignoreFocusOut: true }); // fire-and-forget default language query (async (): Promise => { - if (!defaultLanguage && leetCodeConfig.get("showSetDefaultLanguageHint")) { + if (language && !defaultLanguage && leetCodeConfig.get("showSetDefaultLanguageHint")) { const choice: vscode.MessageItem | undefined = await vscode.window.showInformationMessage( `Would you like to set '${language}' as your default language?`, DialogOptions.yes, diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index 263005c9..241b6251 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -111,7 +111,14 @@ class LeetCodeExecutor { } public async submitSolution(filePath: string): Promise { - return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "submit", `"${filePath}"`]); + try { + return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "submit", `"${filePath}"`]); + } catch (error) { + if (error.result) { + return error.result; + } + throw error; + } } public async testSolution(filePath: string, testString?: string): Promise { diff --git a/src/utils/cpUtils.ts b/src/utils/cpUtils.ts index 6e265e48..59e907b6 100644 --- a/src/utils/cpUtils.ts +++ b/src/utils/cpUtils.ts @@ -5,6 +5,10 @@ import * as cp from "child_process"; import * as vscode from "vscode"; import { leetCodeChannel } from "../leetCodeChannel"; +interface IExecError extends Error { + result?: string; +} + export async function executeCommand(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise { return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => { let result: string = ""; @@ -20,9 +24,14 @@ export async function executeCommand(command: string, args: string[], options: c childProc.stderr.on("data", (data: string | Buffer) => leetCodeChannel.append(data.toString())); childProc.on("error", reject); + childProc.on("close", (code: number) => { if (code !== 0 || result.indexOf("ERROR") > -1) { - reject(new Error(`Command "${command} ${args.toString()}" failed with exit code "${code}".`)); + const error: IExecError = new Error(`Command "${command} ${args.toString()}" failed with exit code "${code}".`); + if (result) { + error.result = result; // leetcode-cli may print useful content by exit with error code + } + reject(error); } else { resolve(result); }