Skip to content

Commit defcf98

Browse files
authored
Merge pull request #6 from MFB-Technologies-Inc/bugfix/5-uncaught-exception
5: improve error handling and logging
2 parents 00ff52f + 0c9f370 commit defcf98

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@mfbtech/changelog-generator",
5+
"comment": "Improve error messages and logging",
6+
"type": "PATCH"
7+
}
8+
]
9+
}

src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,34 @@ import {
1515
} from "./constants.js"
1616
import { publish } from "./commands/publish.js"
1717

18+
// Add global handler for unhandled promise rejections
19+
process.on("unhandledRejection", (reason, promise) => {
20+
console.error("Unhandled promise rejection:", promise, "reason: ", reason)
21+
process.exit(1)
22+
})
23+
1824
const ccgProgram = new Command("ccg")
1925

2026
ccgProgram
2127
.command(ChangeCommandName)
2228
.description(ChangeCommandDescription)
2329
.option(ChangeCommandOptionFlag.verify, ChangeCommandOptionDescription.verify)
24-
.action(change)
30+
.action(args => {
31+
change(args).catch(e => {
32+
console.error(e)
33+
process.exit(1)
34+
})
35+
})
2536

2637
ccgProgram
2738
.command(PublishCommandName)
2839
.description(PublishCommandDescription)
2940
.option(PublishCommandOptionFlag.apply, PublishCommandOptionDescription.apply)
30-
.action(publish)
41+
.action(args => {
42+
publish(args).catch(e => {
43+
console.error(e)
44+
process.exit(1)
45+
})
46+
})
3147

3248
ccgProgram.parse(process.argv)

src/services/processes.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,60 @@ export async function runCommand(args: {
1212
return new Promise((resolve, reject) => {
1313
let result = ""
1414
command.on("error", err => {
15-
reject("command failed: failed to start the subprocess: " + err.message)
15+
reject(
16+
new Error(
17+
"command failed: failed to start the subprocess: " +
18+
err.message +
19+
"\ncommand: " +
20+
args.command +
21+
" " +
22+
args.args?.join(" ")
23+
)
24+
)
1625
})
1726
command.on("close", code => {
1827
if (code !== 0) {
19-
reject("command failed with exit code " + code)
28+
reject(
29+
new Error(
30+
"command failed with exit code " +
31+
code +
32+
"\ncommand: " +
33+
args.command +
34+
" " +
35+
args.args?.join(" ")
36+
)
37+
)
2038
} else {
2139
resolve(result)
2240
}
2341
})
2442
command.stdout.on("data", (data: unknown) => {
2543
if (!hasToStringMethod(data)) {
26-
reject("unknown response data: " + JSON.stringify(data))
44+
reject(
45+
new Error(
46+
"unknown response data: " +
47+
JSON.stringify(data) +
48+
"\ncommand: " +
49+
args.command +
50+
" " +
51+
args.args?.join(" ")
52+
)
53+
)
2754
command.kill(1)
2855
return
2956
}
3057
result += data.toString().trim()
3158
})
3259
command.stderr.on("data", data => {
33-
reject(`command failed: ${data}`)
60+
reject(
61+
new Error(
62+
`command failed: ${data}` +
63+
"\ncommand: " +
64+
args.command +
65+
" " +
66+
args.args?.join(" ")
67+
)
68+
)
3469
command.kill(1)
3570
})
3671
})

0 commit comments

Comments
 (0)