* chore(api.ts): add error handling for openAI api error

* chore(generateCommitMessageFromGitDiff.ts): remove console.log statement
This commit is contained in:
di-sukharev
2023-03-11 13:24:02 +08:00
parent b35a393152
commit 31357132e4
2 changed files with 19 additions and 4 deletions

View File

@@ -1,4 +1,6 @@
import { intro, outro } from '@clack/prompts';
import { AxiosError } from 'axios';
import chalk from 'chalk';
import {
ChatCompletionRequestMessage,
Configuration as OpenAiApiConfiguration,
@@ -48,9 +50,23 @@ class OpenAi {
const message = data.choices[0].message;
return message?.content;
} catch (error) {
// console.error('openAI api error', { error });
throw error;
} catch (error: any) {
outro(`${chalk.red('✖')} ${error}`);
if (error.isAxiosError && error.response?.status === 401) {
const err = error as AxiosError;
const openAiError = (
err.response?.data as { error?: { message: string } }
).error;
if (openAiError?.message) outro(openAiError.message);
outro(
'For help look into README https://github.com/di-sukharev/opencommit#setup'
);
}
process.exit(1);
}
};
}

View File

@@ -105,7 +105,6 @@ export const generateCommitMessageWithChatCompletion = async (
return commitMessage;
}
} catch (error) {
console.log({ error });
return { error: GenerateCommitMessageErrorEnum.internalError };
}
};