Merge pull request #202 from Zagrios/hotfix/better-http-requests-errors-handling

[hotfix] better http error handling
This commit is contained in:
MathieuG-P
2023-04-06 00:29:59 +02:00
committed by GitHub
3 changed files with 16 additions and 12 deletions
+4
View File
@@ -0,0 +1,4 @@
export async function allSettled<T>(promises: Promise<T>[]): Promise<T[]> {
const settledPromises = await Promise.allSettled(promises);
return settledPromises.map(p => p.status === 'fulfilled' ? p.value : null);
}
+11 -11
View File
@@ -3,8 +3,8 @@ import path from 'path';
import { writeFileSync } from 'fs';
import { BSVersion } from 'shared/bs-version.interface';
import { RequestService } from "./request.service"
import isOnline from 'is-online';
import { readJSON } from 'fs-extra';
import { allSettled } from '../helpers/promise.helpers';
export class BSVersionLibService{
@@ -43,16 +43,16 @@ export class BSVersionLibService{
writeFileSync(localVersionsPath, JSON.stringify(versions, null, "\t"), {encoding: 'utf-8', flag: 'w'});
};
private async loadBsVersions(): Promise<BSVersion[]>{
if(this.bsVersions){ return this.bsVersions; }
const [localVersions, remoteVersions] = await Promise.all([
this.getLocalVersions(), (await isOnline({timeout: 1500}) && this.getRemoteVersions())
]);
let resVersions = localVersions;
if(remoteVersions && remoteVersions.length){ resVersions = remoteVersions; this.updateLocalVersions(resVersions); }
this.bsVersions = resVersions;
return this.bsVersions;
}
private async loadBsVersions(): Promise<BSVersion[]>{
if(this.bsVersions){ return this.bsVersions; }
const [localVersions, remoteVersions] = await allSettled([
this.getLocalVersions(), this.getRemoteVersions()
]);
let resVersions = localVersions;
if(remoteVersions && remoteVersions.length){ resVersions = remoteVersions; this.updateLocalVersions(resVersions); }
this.bsVersions = resVersions;
return this.bsVersions;
}
public async getAvailableVersions(): Promise<BSVersion[]>{
const bsVersions = await this.loadBsVersions();
+1 -1
View File
@@ -21,7 +21,7 @@ export class RequestService {
resolve(JSON.parse(body));
});
res.on('error', (err) => reject(err))
});
}).on("error", err => reject(err));
});
}