Fix OpenAI API error handling when generating commit message.
This commit is contained in:
47
aicommits.ts
47
aicommits.ts
@@ -55,30 +55,35 @@ export async function main() {
|
|||||||
console.log(
|
console.log(
|
||||||
chalk.white("▲ ") + chalk.gray("Generating your AI commit message...\n")
|
chalk.white("▲ ") + chalk.gray("Generating your AI commit message...\n")
|
||||||
);
|
);
|
||||||
const aiCommitMessage = await generateCommitMessage(prompt);
|
try {
|
||||||
|
const aiCommitMessage = await generateCommitMessage(prompt);
|
||||||
|
console.log(
|
||||||
|
chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage + "\n"
|
||||||
|
);
|
||||||
|
|
||||||
console.log(
|
const confirmationMessage = await inquirer.prompt([
|
||||||
chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage + "\n"
|
{
|
||||||
);
|
name: "useCommitMessage",
|
||||||
|
message: "Would you like to use this commit message? (Y / n)",
|
||||||
|
choices: ["Y", "y", "n"],
|
||||||
|
default: "y",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
const confirmationMessage = await inquirer.prompt([
|
if (confirmationMessage.useCommitMessage === "n") {
|
||||||
{
|
console.log(chalk.white("▲ ") + "Commit message has not been commited.");
|
||||||
name: "useCommitMessage",
|
process.exit(1);
|
||||||
message: "Would you like to use this commit message? (Y / n)",
|
}
|
||||||
choices: ["Y", "y", "n"],
|
|
||||||
default: "y",
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (confirmationMessage.useCommitMessage === "n") {
|
execSync(`git commit -m "${aiCommitMessage}"`, {
|
||||||
console.log(chalk.white("▲ ") + "Commit message has not been commited.");
|
stdio: "inherit",
|
||||||
|
encoding: "utf8",
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch(e) {
|
||||||
|
console.error(chalk.white("▲ ") + chalk.red(e.message));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
execSync(`git commit -m "${aiCommitMessage}"`, {
|
|
||||||
stdio: "inherit",
|
|
||||||
encoding: "utf8",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function generateCommitMessage(prompt: string) {
|
async function generateCommitMessage(prompt: string) {
|
||||||
@@ -101,6 +106,10 @@ async function generateCommitMessage(prompt: string) {
|
|||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(payload),
|
body: JSON.stringify(payload),
|
||||||
});
|
});
|
||||||
|
if(response.status !== 200) {
|
||||||
|
const errorJson: any = await response.json();
|
||||||
|
throw new Error(`OpenAI API failed while processing the request '${errorJson?.error?.message}'`);
|
||||||
|
}
|
||||||
|
|
||||||
const json: any = await response.json();
|
const json: any = await response.json();
|
||||||
const aiCommit = json.choices[0].text;
|
const aiCommit = json.choices[0].text;
|
||||||
|
|||||||
Reference in New Issue
Block a user