From bc1e809545637765eee61b4b1a5f60cbf607f7bd Mon Sep 17 00:00:00 2001 From: MathieuG-P <40181755+Zagrios@users.noreply.github.com> Date: Thu, 6 Apr 2023 00:29:32 +0200 Subject: [PATCH] [hotfix] better http error handling --- src/main/helpers/promise.helpers.ts | 4 ++++ src/main/services/bs-version-lib.service.ts | 22 ++++++++++----------- src/main/services/request.service.ts | 2 +- 3 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 src/main/helpers/promise.helpers.ts diff --git a/src/main/helpers/promise.helpers.ts b/src/main/helpers/promise.helpers.ts new file mode 100644 index 00000000..21ba61a0 --- /dev/null +++ b/src/main/helpers/promise.helpers.ts @@ -0,0 +1,4 @@ +export async function allSettled(promises: Promise[]): Promise { + const settledPromises = await Promise.allSettled(promises); + return settledPromises.map(p => p.status === 'fulfilled' ? p.value : null); +} \ No newline at end of file diff --git a/src/main/services/bs-version-lib.service.ts b/src/main/services/bs-version-lib.service.ts index 89faaa7e..c58563b0 100644 --- a/src/main/services/bs-version-lib.service.ts +++ b/src/main/services/bs-version-lib.service.ts @@ -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{ - 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{ + 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{ const bsVersions = await this.loadBsVersions(); diff --git a/src/main/services/request.service.ts b/src/main/services/request.service.ts index 5c2c2237..b03eb16f 100644 --- a/src/main/services/request.service.ts +++ b/src/main/services/request.service.ts @@ -21,7 +21,7 @@ export class RequestService { resolve(JSON.parse(body)); }); res.on('error', (err) => reject(err)) - }); + }).on("error", err => reject(err)); }); }