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

@@ -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 <video> element or selector')
throw new IllegalArgumentError('First argument must be a <video> element or selector')
}
if (format == null) {
@@ -14,7 +16,7 @@ function captureVideoFrame (video, format) {
}
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')

View File

@@ -14,6 +14,7 @@ module.exports = {
}
const config = require('../../config')
const {CastingError} = require('./errors')
// Lazy load these for a ~300ms improvement in startup time
let airplayer, chromecasts, dlnacasts
@@ -350,14 +351,16 @@ function toggleMenu (location) {
// Never cast to two devices at the same time
if (state.playing.location !== 'local') {
throw new Error('You can\'t connect to ' + location +
' when already connected to another device')
}
throw new CastingError(
`You can't connect to ${location} when already connected to another device`
) }
// Find all cast devices of the given type
const player = getPlayer(location)
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
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 = {
CastingError,
PlaybackError,
SoundError,
TorrentError,
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 {InvalidSoundNameError} = require('./errors')
const path = require('path')
const VOLUME = 0.15
@@ -62,7 +63,7 @@ function play (name) {
if (!audio) {
const sound = sounds[name]
if (!sound) {
throw new Error('Invalid sound name')
throw new InvalidSoundNameError(name)
}
audio = cache[name] = new window.Audio()
audio.volume = sound.volume