Fix OpenAI API error handling when generating commit message.

This commit is contained in:
يوسف الأوسي
2023-02-14 18:18:22 +10:00
parent e944f68c36
commit de546e9e2e

View File

@@ -55,8 +55,8 @@ 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")
); );
try {
const aiCommitMessage = await generateCommitMessage(prompt); const aiCommitMessage = await generateCommitMessage(prompt);
console.log( console.log(
chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage + "\n" chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage + "\n"
); );
@@ -79,6 +79,11 @@ export async function main() {
stdio: "inherit", stdio: "inherit",
encoding: "utf8", encoding: "utf8",
}); });
} catch(e) {
console.error(chalk.white("▲ ") + chalk.red(e.message));
process.exit(1);
}
} }
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;