Move error definitions to errors.js (#898)

This commit is contained in:
Adam Gotlib
2016-09-07 22:21:59 +02:00
committed by DC
parent d88229694a
commit d331bae548
9 changed files with 76 additions and 26 deletions

View File

@@ -23,6 +23,7 @@
"dlnacasts": "^0.1.0", "dlnacasts": "^0.1.0",
"drag-drop": "^2.12.1", "drag-drop": "^2.12.1",
"electron": "1.3.3", "electron": "1.3.3",
"es6-error": "^3.0.1",
"fs-extra": "^0.30.0", "fs-extra": "^0.30.0",
"iso-639-1": "^1.2.1", "iso-639-1": "^1.2.1",
"languagedetect": "^1.1.1", "languagedetect": "^1.1.1",

View File

@@ -4,7 +4,8 @@ const path = require('path')
const Cast = require('../lib/cast') const Cast = require('../lib/cast')
const {dispatch} = require('../lib/dispatcher') const {dispatch} = require('../lib/dispatcher')
const telemetry = require('../lib/telemetry') const telemetry = require('../lib/telemetry')
const errors = require('../lib/errors') const {UnplayableFileError, UnplayableTorrentError,
PlaybackTimedOutError} = require('../lib/errors')
const sound = require('../lib/sound') const sound = require('../lib/sound')
const TorrentPlayer = require('../lib/torrent-player') const TorrentPlayer = require('../lib/torrent-player')
const TorrentSummary = require('../lib/torrent-summary') const TorrentSummary = require('../lib/torrent-summary')
@@ -42,7 +43,7 @@ module.exports = class PlaybackController {
if (index === undefined || initialized) index = torrentSummary.mostRecentFileIndex if (index === undefined || initialized) index = torrentSummary.mostRecentFileIndex
if (index === undefined) index = torrentSummary.files.findIndex(TorrentPlayer.isPlayable) 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 initialized = true
@@ -232,7 +233,7 @@ module.exports = class PlaybackController {
// TODO: remove torrentSummary.playStatus // TODO: remove torrentSummary.playStatus
torrentSummary.playStatus = 'timeout' /* no seeders available? */ torrentSummary.playStatus = 'timeout' /* no seeders available? */
sound.play('ERROR') sound.play('ERROR')
cb(new Error('Playback timed out. Try again.')) cb(new PlaybackTimedOutError())
this.update() this.update()
}, 10000) /* give it a few seconds */ }, 10000) /* give it a few seconds */
@@ -277,7 +278,7 @@ module.exports = class PlaybackController {
if (!TorrentPlayer.isPlayable(fileSummary)) { if (!TorrentPlayer.isPlayable(fileSummary)) {
torrentSummary.mostRecentFileIndex = undefined torrentSummary.mostRecentFileIndex = undefined
return cb(new Error('Can\'t play that file')) return cb(new UnplayableFileError())
} }
torrentSummary.mostRecentFileIndex = index torrentSummary.mostRecentFileIndex = index

View File

@@ -3,6 +3,7 @@ const path = require('path')
const electron = require('electron') const electron = require('electron')
const {dispatch} = require('../lib/dispatcher') const {dispatch} = require('../lib/dispatcher')
const {TorrentKeyNotFoundError} = require('../lib/errors')
const State = require('../lib/state') const State = require('../lib/state')
const sound = require('../lib/sound') const sound = require('../lib/sound')
const TorrentSummary = require('../lib/torrent-summary') const TorrentSummary = require('../lib/torrent-summary')
@@ -75,7 +76,7 @@ module.exports = class TorrentListController {
// Starts downloading and/or seeding a given torrentSummary. // Starts downloading and/or seeding a given torrentSummary.
startTorrentingSummary (torrentKey) { startTorrentingSummary (torrentKey) {
const s = TorrentSummary.getByKey(this.state, 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 // New torrent: give it a path
if (!s.path) { if (!s.path) {

View File

@@ -1,12 +1,14 @@
module.exports = captureVideoFrame module.exports = captureVideoFrame
const {IllegalArgumentError} = require('./errors')
function captureVideoFrame (video, format) { function captureVideoFrame (video, format) {
if (typeof video === 'string') { if (typeof video === 'string') {
video = document.querySelector(video) video = document.querySelector(video)
} }
if (video == null || video.nodeName !== 'VIDEO') { if (video == null || video.nodeName !== 'VIDEO') {
throw new Error('First argument must be a <video> element or selector') throw new IllegalArgumentError('First argument must be a <video> element or selector')
} }
if (format == null) { if (format == null) {
@@ -14,7 +16,7 @@ function captureVideoFrame (video, format) {
} }
if (format !== 'png' && format !== 'jpg' && format !== 'webp') { if (format !== 'png' && format !== 'jpg' && format !== 'webp') {
throw new Error('Second argument must be one of "png", "jpg", or "webp"') throw new IllegalArgumentError('Second argument must be one of "png", "jpg", or "webp"')
} }
const canvas = document.createElement('canvas') const canvas = document.createElement('canvas')

View File

@@ -14,6 +14,7 @@ module.exports = {
} }
const config = require('../../config') const config = require('../../config')
const {CastingError} = require('./errors')
// Lazy load these for a ~300ms improvement in startup time // Lazy load these for a ~300ms improvement in startup time
let airplayer, chromecasts, dlnacasts let airplayer, chromecasts, dlnacasts
@@ -350,14 +351,16 @@ function toggleMenu (location) {
// Never cast to two devices at the same time // Never cast to two devices at the same time
if (state.playing.location !== 'local') { if (state.playing.location !== 'local') {
throw new Error('You can\'t connect to ' + location + throw new CastingError(
' when already connected to another device') `You can't connect to ${location} when already connected to another device`
} ) }
// Find all cast devices of the given type // Find all cast devices of the given type
const player = getPlayer(location) const player = getPlayer(location)
const devices = player ? player.getDevices() : [] const devices = player ? player.getDevices() : []
if (devices.length === 0) throw new Error('No ' + location + ' devices available') if (devices.length === 0) {
throw new CastingError(`No ${location} devices available`)
}
// Show a menu // Show a menu
state.devices.castMenu = {location, devices} state.devices.castMenu = {location, devices}

View File

@@ -1,15 +1,54 @@
const ExtendableError = require('es6-error')
/* Generic errors */
class CastingError extends ExtendableError {}
class PlaybackError extends ExtendableError {}
class SoundError extends ExtendableError {}
class TorrentError extends ExtendableError {}
/* Playback */
class UnplayableTorrentError extends PlaybackError {
constructor () { super('Can\'t play any files in torrent') }
}
class UnplayableFileError extends PlaybackError {
constructor () { super('Can\'t play that file') }
}
class PlaybackTimedOutError extends PlaybackError {
constructor () { super('Playback timed out. Try again.') }
}
/* Sound */
class InvalidSoundNameError extends SoundError {
constructor (name) { super(`Invalid sound name: ${name}`) }
}
/* Torrent */
class TorrentKeyNotFoundError extends TorrentError {
constructor (torrentKey) { super(`Can't resolve torrent key ${torrentKey}`) }
}
class InvalidTorrentError extends TorrentError {}
/* Miscellaneous */
class IllegalArgumentError extends ExtendableError {}
module.exports = { module.exports = {
CastingError,
PlaybackError,
SoundError,
TorrentError,
UnplayableTorrentError, UnplayableTorrentError,
UnplayableFileError UnplayableFileError,
PlaybackTimedOutError,
InvalidSoundNameError,
TorrentKeyNotFoundError,
InvalidTorrentError,
IllegalArgumentError
} }
function UnplayableTorrentError () {
this.message = 'Can\'t play any files in torrent'
}
function UnplayableFileError () {
this.message = 'Can\'t play that file'
}
UnplayableTorrentError.prototype = Error
UnplayableFileError.prototype = Error

View File

@@ -4,6 +4,7 @@ module.exports = {
} }
const config = require('../../config') const config = require('../../config')
const {InvalidSoundNameError} = require('./errors')
const path = require('path') const path = require('path')
const VOLUME = 0.15 const VOLUME = 0.15
@@ -62,7 +63,7 @@ function play (name) {
if (!audio) { if (!audio) {
const sound = sounds[name] const sound = sounds[name]
if (!sound) { if (!sound) {
throw new Error('Invalid sound name') throw new InvalidSoundNameError(name)
} }
audio = cache[name] = new window.Audio() audio = cache[name] = new window.Audio()
audio.volume = sound.volume audio.volume = sound.volume

View File

@@ -4,6 +4,7 @@ const prettyBytes = require('prettier-bytes')
const TorrentSummary = require('../lib/torrent-summary') const TorrentSummary = require('../lib/torrent-summary')
const TorrentPlayer = require('../lib/torrent-player') const TorrentPlayer = require('../lib/torrent-player')
const {dispatcher} = require('../lib/dispatcher') const {dispatcher} = require('../lib/dispatcher')
const {InvalidTorrentError} = require('../lib/errors')
module.exports = class TorrentList extends React.Component { module.exports = class TorrentList extends React.Component {
render () { render () {
@@ -58,7 +59,7 @@ module.exports = class TorrentList extends React.Component {
if (torrentSummary.playStatus) classes.push(torrentSummary.playStatus) if (torrentSummary.playStatus) classes.push(torrentSummary.playStatus)
if (isSelected) classes.push('selected') if (isSelected) classes.push('selected')
if (!infoHash) classes.push('disabled') if (!infoHash) classes.push('disabled')
if (!torrentSummary.torrentKey) throw new Error('Missing torrentKey') if (!torrentSummary.torrentKey) throw new InvalidTorrentError('Missing torrentKey')
return ( return (
<div <div
key={torrentSummary.torrentKey} key={torrentSummary.torrentKey}

View File

@@ -15,6 +15,7 @@ const zeroFill = require('zero-fill')
const crashReporter = require('../crash-reporter') const crashReporter = require('../crash-reporter')
const config = require('../config') const config = require('../config')
const {TorrentKeyNotFoundError} = require('./lib/errors')
const torrentPoster = require('./lib/torrent-poster') const torrentPoster = require('./lib/torrent-poster')
// Report when the process crashes // Report when the process crashes
@@ -387,7 +388,7 @@ function selectFiles (torrentOrInfoHash, selections) {
// Throws an Error if we're not currently torrenting anything w/ that key // Throws an Error if we're not currently torrenting anything w/ that key
function getTorrent (torrentKey) { function getTorrent (torrentKey) {
const ret = client.torrents.find((x) => x.key === torrentKey) const ret = client.torrents.find((x) => x.key === torrentKey)
if (!ret) throw new Error('missing torrent key ' + torrentKey) if (!ret) throw new TorrentKeyNotFoundError(torrentKey)
return ret return ret
} }