Skip to content

Commit 7631a51

Browse files
committed
Use reject instead throw exception
1 parent 8078c34 commit 7631a51

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/hlsBinaries.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ async function getProjectGhcVersion(
116116
async (progress, token) => {
117117
return new Promise<string>((resolve, reject) => {
118118
const command: string = wrapper + ' --project-ghc-version';
119-
logger.info(`Executing '${command}' in cwd ${dir} to get the project ghc version`);
119+
logger.info(`Executing '${command}' in cwd '${dir}' to get the project or file ghc version`);
120120
token.onCancellationRequested(() => {
121-
logger.warn(`User canceled the executon of '${command}'`);
121+
logger.warn(`User canceled the execution of '${command}'`);
122122
});
123123
// Need to set the encoding to 'utf8' in order to get back a string
124-
// We execute the command in a shell for windows, to allow use cmd or bat scripts
124+
// We execute the command in a shell for windows, to allow use .cmd or .bat scripts
125125
const childProcess = child_process
126126
.execFile(
127127
command,
@@ -130,25 +130,33 @@ async function getProjectGhcVersion(
130130
if (err) {
131131
logger.error(`Error executing '${command}' with error code ${err.code}`);
132132
logger.error(`stderr: ${stderr}`);
133-
logger.error(`stdout: ${stdout}`);
133+
if (stdout) {
134+
logger.error(`stdout: ${stdout}`);
135+
}
134136
const regex = /Cradle requires (.+) but couldn't find it/;
135137
const res = regex.exec(stderr);
136138
if (res) {
137-
throw new MissingToolError(res[1]);
139+
reject(new MissingToolError(res[1]));
138140
}
139-
throw Error(
140-
`${wrapper} --project-ghc-version exited with exit code ${err.code}:\n${stdout}\n${stderr}`
141+
reject(
142+
Error(`${wrapper} --project-ghc-version exited with exit code ${err.code}:\n${stdout}\n${stderr}`)
141143
);
144+
} else {
145+
logger.info(`The GHC version for the project or file: ${stdout?.trim()}`);
146+
resolve(stdout?.trim());
142147
}
143-
resolve(stdout.trim());
144148
}
145149
)
146-
.on('close', (code, signal) => {
147-
logger.info(`Execution of '${command}' closed with code ${code} and signal ${signal}`);
150+
.on('exit', (code, signal) => {
151+
const msg =
152+
`Execution of '${command}' terminated with code ${code}` + (signal ? `and signal ${signal}` : '');
153+
logger.info(msg);
148154
})
149155
.on('error', (err) => {
150-
logger.error(`Error executing '${command}': name = ${err.name}, message = ${err.message}`);
151-
throw err;
156+
if (err) {
157+
logger.error(`Error executing '${command}': name = ${err.name}, message = ${err.message}`);
158+
reject(err);
159+
}
152160
});
153161
token.onCancellationRequested((_) => childProcess.kill());
154162
});
@@ -306,7 +314,7 @@ export async function downloadHaskellLanguageServer(
306314
return null;
307315
}
308316
logger.info(`The latest release is ${release.tag_name}`);
309-
logger.info(`Figure out the ghc version to use or advertise an installation link for missing components`);
317+
logger.info('Figure out the ghc version to use or advertise an installation link for missing components');
310318
const dir: string = folder?.uri?.fsPath ?? path.dirname(resource.fsPath);
311319
let ghcVersion: string;
312320
try {

0 commit comments

Comments
 (0)