diff --git a/src/hooks/useSubmit.ts b/src/hooks/useSubmit.ts index e92b4c5..4ae9cc7 100644 --- a/src/hooks/useSubmit.ts +++ b/src/hooks/useSubmit.ts @@ -23,26 +23,30 @@ const useSubmit = () => { message: MessageInterface[] ): Promise => { let data; - if (!apiKey || apiKey.length === 0) { - // official endpoint - if (apiEndpoint === officialAPIEndpoint) { - throw new Error(t('noApiKeyWarning') as string); - } + try { + if (!apiKey || apiKey.length === 0) { + // official endpoint + if (apiEndpoint === officialAPIEndpoint) { + throw new Error(t('noApiKeyWarning') as string); + } - // other endpoints - data = await getChatCompletion( - useStore.getState().apiEndpoint, - message, - _defaultChatConfig - ); - } else if (apiKey) { - // own apikey - data = await getChatCompletion( - useStore.getState().apiEndpoint, - message, - _defaultChatConfig, - apiKey - ); + // other endpoints + data = await getChatCompletion( + useStore.getState().apiEndpoint, + message, + _defaultChatConfig + ); + } else if (apiKey) { + // own apikey + data = await getChatCompletion( + useStore.getState().apiEndpoint, + message, + _defaultChatConfig, + apiKey + ); + } + } catch (error: unknown) { + throw new Error(`Error generating title!\n${(error as Error).message}`); } return data.choices[0].message.content; };