Fixed api errors and some async issues
This commit is contained in:
parent
3d208cf7d9
commit
ec5d09586f
2 changed files with 5 additions and 5 deletions
|
@ -88,8 +88,8 @@ async function send_this(data) {
|
||||||
options['body'] = JSON.stringify(data.request.payload);
|
options['body'] = JSON.stringify(data.request.payload);
|
||||||
}
|
}
|
||||||
fs.writeFileSync('log', JSON.stringify({event: 'send', date: new Date(), data: data}) + '\n', {flag: 'a+'});
|
fs.writeFileSync('log', JSON.stringify({event: 'send', date: new Date(), data: data}) + '\n', {flag: 'a+'});
|
||||||
let response = await fetch(`https://api.spacetraders.io/v2${data.request.endpoint}`, options);
|
|
||||||
try {
|
try {
|
||||||
|
let response = await fetch(`https://api.spacetraders.io/v2${data.request.endpoint}`, options);
|
||||||
response = await response.json();
|
response = await response.json();
|
||||||
switch(response.error?.code) {
|
switch(response.error?.code) {
|
||||||
//case 401: // TODO 401 means a server reset happened
|
//case 401: // TODO 401 means a server reset happened
|
||||||
|
|
|
@ -15,7 +15,7 @@ export async function extract(ctx) {
|
||||||
switch(response.error.code) {
|
switch(response.error.code) {
|
||||||
case 4000: // ship is on cooldown
|
case 4000: // ship is on cooldown
|
||||||
await api.sleep(response.error.data.cooldown.remainingSeconds * 1000);
|
await api.sleep(response.error.data.cooldown.remainingSeconds * 1000);
|
||||||
return extract(ctx);
|
return await extract(ctx);
|
||||||
case 4228: // ship is full
|
case 4228: // ship is full
|
||||||
return null;
|
return null;
|
||||||
default: // yet unhandled error
|
default: // yet unhandled error
|
||||||
|
@ -38,7 +38,7 @@ export async function dock(ctx) {
|
||||||
switch(response.error.code) {
|
switch(response.error.code) {
|
||||||
case 4214: // ship is in transit
|
case 4214: // ship is in transit
|
||||||
await api.sleep(response.error.data.secondsToArrival * 1000);
|
await api.sleep(response.error.data.secondsToArrival * 1000);
|
||||||
return dock(ctx);
|
return await dock(ctx);
|
||||||
default: // yet unhandled error
|
default: // yet unhandled error
|
||||||
throw response;
|
throw response;
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ export async function navigate(ctx) {
|
||||||
switch(response.error.code) {
|
switch(response.error.code) {
|
||||||
case 4214: // ship is in transit
|
case 4214: // ship is in transit
|
||||||
await api.sleep(response.error.data.secondsToArrival * 1000);
|
await api.sleep(response.error.data.secondsToArrival * 1000);
|
||||||
return navigate(ctx);
|
return await navigate(ctx);
|
||||||
default: // yet unhandled error
|
default: // yet unhandled error
|
||||||
throw response;
|
throw response;
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ export async function orbit(ctx) {
|
||||||
switch(response.error.code) {
|
switch(response.error.code) {
|
||||||
case 4214: // ship is in transit
|
case 4214: // ship is in transit
|
||||||
await api.sleep(response.error.data.secondsToArrival * 1000);
|
await api.sleep(response.error.data.secondsToArrival * 1000);
|
||||||
return orbit(ctx);
|
return await orbit(ctx);
|
||||||
default: // yet unhandled error
|
default: // yet unhandled error
|
||||||
throw response;
|
throw response;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue