Skip to content

Commit 92575ec

Browse files
committed
error handling for streamed data
1 parent 6c743ec commit 92575ec

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/commands/export.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const {
1717
pleaseCaptureLoginTestLog,
1818
enterLoginRouteLog,
1919
testEligibleForExportLog,
20-
jestAuthFileGenerationLog
20+
testExportStartedLog
2121
} = require("../utils/cmdPrint");
2222
const {
2323
getJestAuthFunction,
@@ -102,6 +102,7 @@ function cleanupDataFolder() {
102102
}
103103

104104
async function exportTest(originalTest, exportsMetadata) {
105+
testExportStartedLog();
105106
let test = convertOldTestForGPT(originalTest);
106107
let jestTest = await getJestTest(test);
107108
let testName = await getJestTestName(jestTest, Object.values(exportsMetadata).map(obj => obj.testName));

src/helpers/api.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const https = require('https');
22
const _ = require('lodash');
33
const axios = require('axios');
4-
const {} = require('../utils/cmdPrint');
4+
const { jestAuthFileGenerationLog } = require('../utils/cmdPrint');
55
const args = require('../utils/getArgs.js');
66

77
function extractGPTMessageFromStreamData(input) {
@@ -38,12 +38,19 @@ function setOptions({protocol, hostname, port, path, method, headers}) {
3838

3939
async function makeRequest(data, options) {
4040
let gptResponse = '';
41+
let end = false;
4142

4243
return new Promise((resolve, reject) => {
4344
const req = https.request(_.omit(options, ['protocol']), function(res){
4445
res.on('data', (chunk) => {
4546
try {
4647
let stringified = chunk.toString();
48+
if (stringified === 'pythagora_end') {
49+
gptResponse = '';
50+
end = true;
51+
return;
52+
}
53+
if (end) return gptResponse = stringified;
4754
try {
4855
let json = JSON.parse(stringified);
4956
if (json.error) gptResponse = json.error;
@@ -60,8 +67,9 @@ async function makeRequest(data, options) {
6067

6168
} catch (e) {}
6269
});
63-
res.on('end', async () => {
64-
if (res.statusCode >= 400) throw new Error(`Response status code: ${res.statusCode}.`);
70+
res.on('end', async (a,b,c) => {
71+
process.stdout.write('\n');
72+
if (res.statusCode >= 400) throw new Error(`Response status code: ${res.statusCode}. Error message: ${gptResponse}`);
6573
if (gptResponse.message) throw new Error(`Error: ${gptResponse.message}. Code: ${gptResponse.code}`);
6674
gptResponse = cleanupGPTResponse(gptResponse);
6775
resolve(gptResponse);
@@ -102,7 +110,7 @@ async function isEligibleForExport(jestTest) {
102110
let options = setOptions({ path: '/check-if-eligible' });
103111

104112
const response = await axios.post(
105-
`${options.protocol}://${options.hostname}:${options.port}${options.path}`,
113+
`${options.protocol}://${options.hostname}${options.port ? ':' + options.port : ''}${options.path}`,
106114
JSON.stringify({ jestTest }),
107115
{ headers: options.headers }
108116
);

src/utils/cmdPrint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function pleaseCaptureLoginTestLog(loginEndpointPath) {
182182
console.log(`Login path is: ${green}${bold}${loginEndpointPath}${reset}`);
183183
console.log('Please run the Pythagora capture command, log into your app and try the export again.');
184184
console.log('To start Pythagora capture, run:');
185-
console.log(`${blue}${bold}npx pythagora --init-command \"${args.init_command.join(' ')}\" --mode capture${reset}`);
185+
console.log(`${blue}${bold}npx pythagora --init-command \"my start command\" --mode capture${reset}`);
186186
}
187187

188188
function logLoginEndpointCaptured() {

0 commit comments

Comments
 (0)