diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2014-05-18 14:45:45 +0400 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2014-05-18 14:45:45 +0400 |
commit | 1c614d171f63009f5f516dab5b9de60cfc28bc05 (patch) | |
tree | bdf30ff2de85736b466ea051d9387fab7f769832 /index.js | |
parent | 2c41b00c47c16a0a09c5398de5b84f4e35eafa08 (diff) |
emit 'error' events for torrent parse errors
Diffstat (limited to 'index.js')
-rw-r--r-- | index.js | 15 |
1 files changed, 11 insertions, 4 deletions
@@ -9,6 +9,7 @@ var FSStorage = require('./lib/fs_storage') var http = require('http') var inherits = require('inherits') var mime = require('mime') +var once = require('once') var pump = require('pump') var rangeParser = require('range-parser') var url = require('url') @@ -48,6 +49,7 @@ WebTorrent.prototype.add = function (torrentId, opts, cb) { if (typeof cb !== 'function') { cb = function () {} } + cb = once(cb) opts = extend({ storage: FSStorage @@ -72,16 +74,21 @@ WebTorrent.prototype.add = function (torrentId, opts, cb) { onTorrentId(torrent) })) }).on('error', function (err) { - cb(new Error('Error downloading torrent from ' + torrentId + '\n' + err.message)) + err = new Error('Error downloading torrent from ' + torrentId + '\n' + err.message) + cb(err) + self.emit('error', err) }) } else { // assume it's a filesystem path fs.readFile(torrentId, function (err, torrent) { if (err) { - return cb(new Error('Cannot add torrent "' + torrentId + '". Torrent id must be one of: magnet uri, ' + - 'info hash, torrent file, http url, or filesystem path.')) + err = new Error('Cannot add torrent "' + torrentId + '". Torrent id must be one of: magnet uri, ' + + 'info hash, torrent file, http url, or filesystem path.') + cb(err) + self.emit('error', err) + } else { + onTorrentId(torrent) } - onTorrentId(torrent) }) } |