diff options
Diffstat (limited to '')
-rw-r--r-- | lib/api.js | 25 |
1 files changed, 18 insertions, 7 deletions
@@ -63,20 +63,31 @@ function send_this(data) { fs.writeFileSync('log', JSON.stringify({event: 'send', date: new Date(), data: data}) + '\n', {flag: 'a+'}); fetch(`https://api.spacetraders.io/v2${data.request.endpoint}`, options) .then(response => response.json()) - .then(response => { - fs.writeFileSync('log', JSON.stringify({event: 'response', date: new Date(), data: response}) + '\n', {flag: 'a+'}); - return data.resolve(response);}) - .catch(err => { + .then(async response => { + switch(response.error?.code) { + case 429: // 429 means rate limited, let's hold back for 10 seconds + await sleep(10000); + queue.enqueue(data, 1); + break; + default: + fs.writeFileSync('log', JSON.stringify({event: 'response', date: new Date(), data: response}) + '\n', {flag: 'a+'}); + return data.resolve(response); + }}) + .catch(async err => { fs.writeFileSync('log', JSON.stringify({event: 'error', date: new Date(), data: err}) + '\n', {flag: 'a+'}); switch(err.cause?.code) { + case 503: // 503 means maintenance mode, let's hold back for 1 minute + await sleep(60000); + queue.enqueue(data, 1); + break; case 'ECONNRESET': - send_this(data); + queue.enqueue(data, 1); break; case 'UND_ERR_CONNECT_TIMEOUT': - send_this(data); + queue.enqueue(data, 1); break; default: - data.reject(err) + data.reject(err); }}); setTimeout(send_next, 400); // 333 should work, but 400 will still allow manual requests to go through during development } |