Merge pull request #12 from yusefren/fix/when-open-ai-error
Fix OpenAI API error handling when generating commit message
This commit is contained in:
16
aicommits.ts
16
aicommits.ts
@@ -55,10 +55,12 @@ export async function main() {
|
||||
console.log(
|
||||
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"
|
||||
chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage +
|
||||
"\n",
|
||||
);
|
||||
|
||||
const confirmationMessage = await inquirer.prompt([
|
||||
@@ -79,6 +81,10 @@ export async function main() {
|
||||
stdio: "inherit",
|
||||
encoding: "utf8",
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(chalk.white("▲ ") + chalk.red(e.message));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
async function generateCommitMessage(prompt: string) {
|
||||
@@ -101,6 +107,12 @@ async function generateCommitMessage(prompt: string) {
|
||||
method: "POST",
|
||||
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 aiCommit = json.choices[0].text;
|
||||
|
||||
Reference in New Issue
Block a user