Refactor error handling and response handling in BatchHTTP module
This commit is contained in:
@@ -91,7 +91,12 @@ function DoRequest(request)
|
||||
end
|
||||
end
|
||||
|
||||
print("[BatchHTTP] got response",id)
|
||||
if response.error then
|
||||
warn('[BatchHTTP] request '..id..' errored "'..response.error.message..'"')
|
||||
else
|
||||
print("[BatchHTTP] got response "+response.status,id)
|
||||
end
|
||||
|
||||
return response.data
|
||||
end
|
||||
|
||||
|
||||
24
index.js
24
index.js
@@ -29,9 +29,11 @@ async function main() {
|
||||
}
|
||||
responses[instanceId].push({
|
||||
id: response.id,
|
||||
data: response.data
|
||||
data: response.data,
|
||||
error: response.error,
|
||||
status: response.status
|
||||
})
|
||||
console.log(`Response registered for instance ${instanceId}`);
|
||||
//console.log(`Response registered for instance ${instanceId}`);
|
||||
setTimeout(() => {
|
||||
if (!responses[instanceId]) {
|
||||
return;
|
||||
@@ -41,7 +43,7 @@ async function main() {
|
||||
}, 60_000);
|
||||
}
|
||||
|
||||
async function DoRequest(instanceId, requestObj) {
|
||||
async function DoRequest(instanceId, requestObj, retriesLeft = 3) {
|
||||
const requestParams = requestObj.request || {};
|
||||
const method = requestParams.Method?.toLowerCase() || 'get';
|
||||
const url = requestParams.Url || '';
|
||||
@@ -70,16 +72,20 @@ async function main() {
|
||||
data: response.data
|
||||
});
|
||||
} catch (error) {
|
||||
if (RETRY_ERROR_CODES.includes(error.response?.status)) {
|
||||
console.log(`Retrying request ${requestObj.id} for instance ${instanceId}`);
|
||||
SetTimeout(() => {
|
||||
DoRequest(instanceId, requestObj);
|
||||
}, 5000);
|
||||
const statusCode = error.response?.status;
|
||||
if (RETRY_ERROR_CODES.includes(statusCode) && retriesLeft > 0) {
|
||||
console.log(`Retrying request ${requestObj.id} for instance ${instanceId}, HTTP ${statusCode}`);
|
||||
setTimeout(() => {
|
||||
DoRequest(instanceId, requestObj, retriesLeft - 1);
|
||||
}, 1000);
|
||||
} else {
|
||||
RegisterResponse(instanceId, {
|
||||
id: requestObj.id,
|
||||
data: error.response?.data,
|
||||
error: "yep there was an error"
|
||||
status: statusCode,
|
||||
error: {
|
||||
message: error.message || 'Unknown error',
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user