Refactor error handling in main function and improve response handling
This commit is contained in:
41
index.js
41
index.js
@@ -28,6 +28,13 @@ async function main() {
|
||||
data: response.data
|
||||
})
|
||||
console.log(`Response registered for instance ${instanceId}`);
|
||||
setTimeout(() => {
|
||||
if (!responses[instanceId]) {
|
||||
return;
|
||||
}
|
||||
responses[instanceId] = responses[instanceId].filter(resp => resp.id !== response.id);
|
||||
//console.log(`Response ${response.id} removed from instance ${instanceId}`);
|
||||
}, 60_000);
|
||||
}
|
||||
|
||||
async function DoRequest(instanceId, requestObj) {
|
||||
@@ -38,13 +45,13 @@ async function main() {
|
||||
|
||||
let headers = requestParams.Headers || null;
|
||||
// if headers is a an array or is an empty object, convert it to null
|
||||
if (Array.isArray(headers) || Object.keys(headers).length === 0) {
|
||||
if (headers && (Array.isArray(headers) || Object.keys(headers).length === 0)) {
|
||||
headers = null;
|
||||
}
|
||||
|
||||
const shouldHaveBody = ['post', 'put', 'patch'].includes(method);
|
||||
|
||||
console.log(instanceId,requestObj)
|
||||
console.log("DoRequest",instanceId,requestObj)
|
||||
|
||||
try {
|
||||
const response = await axios({
|
||||
@@ -59,7 +66,7 @@ async function main() {
|
||||
data: response.data
|
||||
});
|
||||
} catch (error) {
|
||||
if (RETRY_ERROR_CODES.includes(error.response.status)) {
|
||||
if (RETRY_ERROR_CODES.includes(error.response?.status)) {
|
||||
console.log(`Retrying request ${requestObj.id} for instance ${instanceId}`);
|
||||
SetTimeout(() => {
|
||||
DoRequest(instanceId, requestObj);
|
||||
@@ -67,13 +74,23 @@ async function main() {
|
||||
} else {
|
||||
RegisterResponse(instanceId, {
|
||||
id: requestObj.id,
|
||||
data: error.response.data,
|
||||
data: error.response?.data,
|
||||
error: "yep there was an error"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getToSend(instanceId, waiting) {
|
||||
const toSend = [];
|
||||
const instanceResponses = responses[instanceId] || [];
|
||||
instanceResponses.forEach(respObj => {
|
||||
if (waiting.includes(respObj.id)) {
|
||||
toSend.push(respObj);
|
||||
}
|
||||
});
|
||||
return toSend;
|
||||
}
|
||||
|
||||
app.post('/:instanceId', async (req, res) => {
|
||||
const instanceId = req.params.instanceId;
|
||||
@@ -87,15 +104,15 @@ async function main() {
|
||||
DoRequest(instanceId,requestObj);
|
||||
});
|
||||
|
||||
const toSend = [];
|
||||
const instanceResponses = responses[instanceId] || [];
|
||||
//console.log(instanceResponses,waiting);
|
||||
instanceResponses.forEach(respObj => {
|
||||
if (waiting.includes(respObj.id)) {
|
||||
toSend.push(respObj);
|
||||
console.log(`Response sent for instance ${instanceId}`);
|
||||
const startTime = Date.now();
|
||||
|
||||
let toSend = []
|
||||
while (toSend.length === 0 && Date.now() - startTime < 1_000) {
|
||||
toSend = getToSend(instanceId, waiting);
|
||||
await new Promise(resolve => setTimeout(resolve, 10));
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`${toSend.length} responses ready for instance ${instanceId}`);
|
||||
|
||||
res.send({
|
||||
responses: toSend
|
||||
|
||||
Reference in New Issue
Block a user