From 600e22a6d60dfb669becd4d5a2e7d0b54ba1e764 Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Mon, 2 Nov 2020 21:13:40 +0800 Subject: [PATCH] server: services: refrain from using unnecessary throws --- server/services/feedService.ts | 16 +++++----- .../qBittorrent/clientGatewayService.ts | 2 +- .../services/rTorrent/clientGatewayService.ts | 30 +++++++++---------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/server/services/feedService.ts b/server/services/feedService.ts index 32021d5a..560e9ca1 100644 --- a/server/services/feedService.ts +++ b/server/services/feedService.ts @@ -35,11 +35,11 @@ class FeedService extends BaseService { */ async addFeed({url, label, interval}: AddFeedOptions): Promise { if (typeof url !== 'string' || typeof label !== 'string' || typeof interval !== 'number') { - throw new Error('Unprocessable Entity'); + return Promise.reject(); } if (this.db == null) { - throw new Error(''); + return Promise.reject(); } const newFeed = await new Promise((resolve, reject) => { @@ -67,21 +67,21 @@ class FeedService extends BaseService { */ async modifyFeed(id: string, {url, label, interval}: ModifyFeedOptions): Promise { if (url != null && typeof url !== 'string') { - throw new Error(); + return Promise.reject(); } if (label != null && typeof label !== 'string') { - throw new Error(); + return Promise.reject(); } if (interval != null && typeof interval !== 'number') { - throw new Error(); + return Promise.reject(); } const modifiedFeedReader = this.feedReaders.find((feedReader) => feedReader.getOptions().feedID === id); if (modifiedFeedReader == null || this.db == null) { - throw new Error(); + return Promise.reject(); } // JSON.parse(JSON.stringify()) to remove undefined properties @@ -132,7 +132,7 @@ class FeedService extends BaseService { async getAll(): Promise<{feeds: Array; rules: Array}> { if (this.db == null) { - throw new Error(); + return Promise.reject(); } return new Promise<{feeds: Array; rules: Array}>((resolve, reject) => { @@ -179,7 +179,7 @@ class FeedService extends BaseService { const selectedFeedReader = this.feedReaders.find((feedReader) => feedReader.getOptions().feedID === id); if (selectedFeedReader == null) { - throw new Error(); + return Promise.reject(); } const items = selectedFeedReader.getItems(); diff --git a/server/services/qBittorrent/clientGatewayService.ts b/server/services/qBittorrent/clientGatewayService.ts index 756241ad..0412e7ee 100644 --- a/server/services/qBittorrent/clientGatewayService.ts +++ b/server/services/qBittorrent/clientGatewayService.ts @@ -385,7 +385,7 @@ class QBittorrentClientGatewayService extends ClientGatewayService { async testGateway(clientSettings?: ClientConnectionSettings): Promise { if (clientSettings != null && clientSettings.client !== 'qBittorrent') { - throw new Error(); + return Promise.reject(); } return this.clientRequestManager diff --git a/server/services/rTorrent/clientGatewayService.ts b/server/services/rTorrent/clientGatewayService.ts index df521bdb..5be67aa1 100644 --- a/server/services/rTorrent/clientGatewayService.ts +++ b/server/services/rTorrent/clientGatewayService.ts @@ -64,7 +64,7 @@ class RTorrentClientGatewayService extends ClientGatewayService { start, }: AddTorrentByFileOptions): Promise { if (!Array.isArray(files)) { - throw new Error(); + return Promise.reject(); } const torrentPaths = await Promise.all( @@ -249,16 +249,14 @@ class RTorrentClientGatewayService extends ClientGatewayService { return accumulator; }, []); - await this.stopTorrents({hashes}).catch((e) => { - throw e; - }); - - await this.clientRequestManager - .methodCall('system.multicall', [methodCalls]) - .then(this.processClientRequestSuccess, this.processClientRequestError) - .catch((e) => { - throw e; - }); + try { + await this.stopTorrents({hashes}); + await this.clientRequestManager + .methodCall('system.multicall', [methodCalls]) + .then(this.processClientRequestSuccess, this.processClientRequestError); + } catch (e) { + return Promise.reject(e); + } if (moveFiles) { hashes.forEach((hash) => { @@ -266,7 +264,7 @@ class RTorrentClientGatewayService extends ClientGatewayService { const baseFileName = this.services?.torrentService.getTorrent(hash).baseFilename; if (sourceBasePath == null || baseFileName == null) { - throw new Error(); + return; } const destinationFilePath = path.join(destination, baseFileName); @@ -282,9 +280,11 @@ class RTorrentClientGatewayService extends ClientGatewayService { } if (isCheckHash) { - await this.checkTorrents({hashes}).catch((e) => { - throw e; - }); + try { + await this.checkTorrents({hashes}); + } catch (e) { + return Promise.reject(e); + } } return this.startTorrents({hashes: hashesToRestart});