Skip to content

Commit d639338

Browse files
committed
Merge branch 'QCD-342-2023-06-30_nik_Enhance-server-error-handling-in-Node-SDK'
2 parents 4e49f3d + 4c7d152 commit d639338

2 files changed

Lines changed: 116 additions & 21 deletions

File tree

QencodeApiClient.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ const querystring = require('querystring');
33

44
const TranscodingTask = require('./Classes/TranscodingTask');
55

6+
function delay(time) {
7+
return new Promise(resolve => setTimeout(resolve, time));
8+
}
9+
610
class QencodeApiClient {
711
constructor(options) {
812

@@ -80,9 +84,10 @@ class QencodeApiClient {
8084
}
8185
return new TranscodingTask(this, response.task_token, response.upload_url);
8286
}
83-
84-
async Request(path, parameters, statusUrl){
85-
87+
88+
89+
async Request(path, parameters, statusUrl){
90+
8691
this.lastResponseRaw = null;
8792
this.lastResponse = null;
8893

@@ -103,25 +108,32 @@ class QencodeApiClient {
103108
// convert parameters to string like 'api_key=5adb0584aa29f'
104109
parameters = querystring.stringify(parameters);
105110
}
106-
107-
try {
108-
109-
this.lastResponseRaw = await axios.post(
110-
requestUrl,
111-
parameters,
112-
{
113-
headers: {
114-
'Content-Type': 'application/x-www-form-urlencoded'
115-
}
116-
}
111+
112+
let retry = 20;
113+
const retryDelay = 3000;
117114

118-
);
119-
120-
121-
} catch (err) {
122-
throw new Error("Error executing request to url: " + requestUrl, err);
123-
}
124-
115+
for (let i = 0; i < retry; i++) {
116+
try {
117+
this.lastResponseRaw = await axios.post(
118+
requestUrl,
119+
parameters,
120+
{
121+
headers: {
122+
'Content-Type': 'application/x-www-form-urlencoded'
123+
},
124+
timeout: this.ConnectTimeout * 1000
125+
}
126+
);
127+
break;
128+
} catch (err) {
129+
if(i === retry - 1) {
130+
throw new Error("All attempts to execute request failed.");
131+
}
132+
console.error(`Error executing request to url: ${requestUrl} on attempt ${i + 1}. Retrying in ${retryDelay / 1000} seconds...`);
133+
await delay(retryDelay);
134+
}
135+
}
136+
125137
let response = this.lastResponseRaw.data;
126138

127139
if (response == null || response.error == null){

package-lock.json

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)