Skip to content

Commit 35346ee

Browse files
committed
added error handling for gpt requests
1 parent 9a6554b commit 35346ee

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/commands/export.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ async function exportTest(originalTest, exportsMetadata) {
105105
let test = convertOldTestForGPT(originalTest);
106106
let jestTest = await getJestTest(test);
107107
let testName = await getJestTestName(jestTest, Object.values(exportsMetadata).map(obj => obj.testName));
108+
if (!jestTest && !testName) return console.error('There was issue with getting GPT response. Make sure you have access to GPT4 with your API key.');
109+
108110
fs.writeFileSync(`./${EXPORTED_TESTS_DATA_DIR}/${testName.replace('.test.js', '.json')}`, JSON.stringify(test.mongoQueries, null, 2));
109111
fs.writeFileSync(`./${EXPORTED_TESTS_DIR}/${testName}`, jestTest.replace(test.testId, testName));
110112

src/helpers/api.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const http = require('http');
1+
const https = require('https');
22
const _ = require('lodash');
33
const axios = require('axios');
44
const {} = require('../utils/cmdPrint');
@@ -17,14 +17,15 @@ function extractGPTMessageFromStreamData(input) {
1717

1818
function setOptions({protocol, hostname, port, path, method, headers}) {
1919
let options = {
20-
protocol: protocol || 'http',
21-
hostname: hostname || 'localhost',
20+
protocol: protocol || 'https',
21+
hostname: hostname || 'api.pythagora.io',
2222
port: port || process.env.PORT,
2323
path: path || '/',
2424
method: method || 'POST',
2525
headers: headers || {
2626
'Content-Type': 'application/json',
27-
'Authorization': 'Bearer ' + process.env.OPENAI_API_KEY
27+
'apikey': process.env.OPENAI_API_KEY || process.env.PYTHAGORA_API_KEY,
28+
'apikeytype': process.env.OPENAI_API_KEY ? 'openai' : 'pythagora'
2829
},
2930
};
3031

@@ -36,10 +37,16 @@ async function makeRequest(data, options) {
3637
let gptResponse = '';
3738

3839
return new Promise((resolve, reject) => {
39-
const req = http.request(_.omit(options, ['protocol']), function(res){
40+
const req = https.request(_.omit(options, ['protocol']), function(res){
4041
res.on('data', (chunk) => {
4142
try {
42-
let receivedMessages = extractGPTMessageFromStreamData(chunk.toString());
43+
let stringified = chunk.toString();
44+
try {
45+
let json = JSON.parse(stringified);
46+
if (json.error) gptResponse = json.error;
47+
return;
48+
} catch (e) {}
49+
let receivedMessages = extractGPTMessageFromStreamData(stringified);
4350
receivedMessages.forEach(rm => {
4451
let content = _.get(rm, 'choices.0.delta.content');
4552
if (content) {
@@ -51,6 +58,8 @@ async function makeRequest(data, options) {
5158
} catch (e) {}
5259
});
5360
res.on('end', async () => {
61+
if (res.statusCode >= 400) throw new Error(`Response status code: ${res.statusCode}.`);
62+
if (gptResponse.message) throw new Error(`Error: ${gptResponse.message}. Code: ${gptResponse.code}`);
5463
gptResponse = cleanupGPTResponse(gptResponse);
5564
resolve(gptResponse);
5665
});

0 commit comments

Comments
 (0)