From d331bae548433ab3ba80096af07555b6f02619bb Mon Sep 17 00:00:00 2001 From: Adam Gotlib Date: Wed, 7 Sep 2016 22:21:59 +0200 Subject: [PATCH] Move error definitions to `errors.js` (#898) --- package.json | 1 + .../controllers/playback-controller.js | 9 +-- .../controllers/torrent-list-controller.js | 3 +- src/renderer/lib/capture-video-frame.js | 6 +- src/renderer/lib/cast.js | 11 ++-- src/renderer/lib/errors.js | 63 +++++++++++++++---- src/renderer/lib/sound.js | 3 +- src/renderer/pages/torrent-list-page.js | 3 +- src/renderer/webtorrent.js | 3 +- 9 files changed, 76 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index e119bcaf..d9abba90 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "dlnacasts": "^0.1.0", "drag-drop": "^2.12.1", "electron": "1.3.3", + "es6-error": "^3.0.1", "fs-extra": "^0.30.0", "iso-639-1": "^1.2.1", "languagedetect": "^1.1.1", diff --git a/src/renderer/controllers/playback-controller.js b/src/renderer/controllers/playback-controller.js index e88be8e1..bd517e86 100644 --- a/src/renderer/controllers/playback-controller.js +++ b/src/renderer/controllers/playback-controller.js @@ -4,7 +4,8 @@ const path = require('path') const Cast = require('../lib/cast') const {dispatch} = require('../lib/dispatcher') const telemetry = require('../lib/telemetry') -const errors = require('../lib/errors') +const {UnplayableFileError, UnplayableTorrentError, + PlaybackTimedOutError} = require('../lib/errors') const sound = require('../lib/sound') const TorrentPlayer = require('../lib/torrent-player') const TorrentSummary = require('../lib/torrent-summary') @@ -42,7 +43,7 @@ module.exports = class PlaybackController { if (index === undefined || initialized) index = torrentSummary.mostRecentFileIndex if (index === undefined) index = torrentSummary.files.findIndex(TorrentPlayer.isPlayable) - if (index === undefined) return cb(new errors.UnplayableError()) + if (index === undefined) return cb(new UnplayableTorrentError()) initialized = true @@ -232,7 +233,7 @@ module.exports = class PlaybackController { // TODO: remove torrentSummary.playStatus torrentSummary.playStatus = 'timeout' /* no seeders available? */ sound.play('ERROR') - cb(new Error('Playback timed out. Try again.')) + cb(new PlaybackTimedOutError()) this.update() }, 10000) /* give it a few seconds */ @@ -277,7 +278,7 @@ module.exports = class PlaybackController { if (!TorrentPlayer.isPlayable(fileSummary)) { torrentSummary.mostRecentFileIndex = undefined - return cb(new Error('Can\'t play that file')) + return cb(new UnplayableFileError()) } torrentSummary.mostRecentFileIndex = index diff --git a/src/renderer/controllers/torrent-list-controller.js b/src/renderer/controllers/torrent-list-controller.js index a03343ed..9aeb06cb 100644 --- a/src/renderer/controllers/torrent-list-controller.js +++ b/src/renderer/controllers/torrent-list-controller.js @@ -3,6 +3,7 @@ const path = require('path') const electron = require('electron') const {dispatch} = require('../lib/dispatcher') +const {TorrentKeyNotFoundError} = require('../lib/errors') const State = require('../lib/state') const sound = require('../lib/sound') const TorrentSummary = require('../lib/torrent-summary') @@ -75,7 +76,7 @@ module.exports = class TorrentListController { // Starts downloading and/or seeding a given torrentSummary. startTorrentingSummary (torrentKey) { const s = TorrentSummary.getByKey(this.state, torrentKey) - if (!s) throw new Error('Missing key: ' + torrentKey) + if (!s) throw new TorrentKeyNotFoundError(torrentKey) // New torrent: give it a path if (!s.path) { diff --git a/src/renderer/lib/capture-video-frame.js b/src/renderer/lib/capture-video-frame.js index b6a04674..06ff8b6a 100644 --- a/src/renderer/lib/capture-video-frame.js +++ b/src/renderer/lib/capture-video-frame.js @@ -1,12 +1,14 @@ module.exports = captureVideoFrame +const {IllegalArgumentError} = require('./errors') + function captureVideoFrame (video, format) { if (typeof video === 'string') { video = document.querySelector(video) } if (video == null || video.nodeName !== 'VIDEO') { - throw new Error('First argument must be a