From f305c1d3acfddaec5e98a408a368a7adb8a487c9 Mon Sep 17 00:00:00 2001 From: yann300 Date: Thu, 31 Aug 2023 16:42:27 +0200 Subject: [PATCH] make the code safer --- apps/remix-ide/src/app/plugins/openaigpt.tsx | 35 ++++++++++++-------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/apps/remix-ide/src/app/plugins/openaigpt.tsx b/apps/remix-ide/src/app/plugins/openaigpt.tsx index 6d0a52667f2..042c6da13b4 100644 --- a/apps/remix-ide/src/app/plugins/openaigpt.tsx +++ b/apps/remix-ide/src/app/plugins/openaigpt.tsx @@ -19,19 +19,28 @@ export class OpenAIGpt extends Plugin { async message(prompt): Promise { this.call('terminal', 'log', 'Waiting for GPT answer...') - const result = await ( - await fetch('https://openai-gpt.remixproject.org', { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ prompt }), - }) - ).json() - - console.log(result) - this.call('terminal', 'log', { type: 'typewritersuccess', value: result.choices[0].message.content }) + let result + try { + result = await ( + await fetch('https://openai-gpt.remixproject.org', { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ prompt }), + }) + ).json() + } catch (e) { + this.call('terminal', 'log', { type: 'typewritererror', value: `Unable to get a response ${e.message}` }) + return + } + + if (result && result.choices && result.choices.length) { + this.call('terminal', 'log', { type: 'typewritersuccess', value: result.choices[0].message.content }) + } else { + this.call('terminal', 'log', { type: 'typewritersuccess', value: 'No response...' }) + } return result.data } }