Skip to content

Commit 9026f47

Browse files
committed
refactor: replace empty catch blocks with descriptive comments
Empty catch blocks silently swallow errors, making debugging harder and violating code quality best practices. This adds explanatory comments to all empty catch blocks in core.ts and cli.ts, and replaces the abrupt process.exit(1) in readScriptFromHttp with a proper Fail error for consistent error handling. Ref #1394
1 parent 97e975c commit 9026f47

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/cli.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ function linkNodeModules(cwd: string, external: string): string {
193193
function lstat(p: string) {
194194
try {
195195
return fs.lstatSync(p)
196-
} catch {}
196+
} catch {
197+
// File does not exist — return undefined.
198+
}
197199
}
198200

199201
async function readScript() {
@@ -247,7 +249,8 @@ async function readScriptFromHttp(remote: string): Promise<string> {
247249
const res = await fetch(remote)
248250
if (!res.ok) {
249251
console.error(`Error: Can't get ${remote}`)
250-
process.exit(1)
252+
process.exitCode = 1
253+
throw new Fail(`Failed to fetch remote script: ${remote} (${res.status})`)
251254
}
252255
return res.text()
253256
}

src/core.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,9 @@ try {
10051005
if (isString(shell)) $.shell = shell
10061006
if (isString(prefix)) $.prefix = prefix
10071007
if (isString(postfix)) $.postfix = postfix
1008-
} catch (err) {}
1008+
} catch (err) {
1009+
// Bash may not be available (e.g. on Windows). Fall back to the default shell.
1010+
}
10091011

10101012
let cwdSyncHook: AsyncHook
10111013

@@ -1056,17 +1058,25 @@ export async function kill(
10561058
)
10571059
return
10581060

1061+
// Kill all child processes in the tree. Errors are expected here since
1062+
// processes may have already exited by the time we attempt to kill them.
10591063
for (const p of await ps.tree({ pid, recursive: true })) {
10601064
try {
10611065
process.kill(+p.pid, signal)
1062-
} catch (e) {}
1066+
} catch (e) {
1067+
// Process already exited.
1068+
}
10631069
}
10641070
try {
1071+
// Try killing the process group first (negative pid).
10651072
process.kill(-pid, signal)
10661073
} catch (e) {
10671074
try {
1075+
// If process group kill fails, try killing the process directly.
10681076
process.kill(+pid, signal)
1069-
} catch (e) {}
1077+
} catch (e) {
1078+
// Process already exited.
1079+
}
10701080
}
10711081
}
10721082

0 commit comments

Comments
 (0)