Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If there is a parsing error, We were unable to locate which torrent error. #840

Closed
starzou opened this issue Jun 16, 2016 · 3 comments
Closed
Labels

Comments

@starzou
Copy link

@starzou starzou commented Jun 16, 2016

  • WebTorrent version: 0.94.4
  • Node.js : 6.2.0

In https://github.com/feross/webtorrent/blob/master/lib/torrent.js#L235

Torrent.prototype._onTorrentId = function (torrentId) {
  var self = this
  if (self.destroyed) return

  var parsedTorrent
  try { parsedTorrent = parseTorrent(torrentId) } catch (err) {}
  if (parsedTorrent) {
    // Attempt to set infoHash property synchronously
    self.infoHash = parsedTorrent.infoHash
    process.nextTick(function () {
      if (self.destroyed) return
      self._onParsedTorrent(parsedTorrent)
    })
  } else {
    // If torrentId failed to parse, it could be in a form that requires an async
    // operation, i.e. http/https link, filesystem path, or Blob.
    parseTorrent.remote(torrentId, function (err, parsedTorrent) {
      if (self.destroyed) return
      if (err) return self._destroy(err)
      self._onParsedTorrent(parsedTorrent)
    })
  }
}

In https://github.com/feross/webtorrent/blob/master/lib/torrent.js#L679

  if (err) {
    // Torrent errors are emitted at `torrent.on('error')`. If there are no 'error' event
    // handlers on the torrent instance, the error will be emitted at
    // `client.on('error')`. This prevents crashing the user's program, but it makes it
    // impossible to determine a client error versus a torrent error (where the client
    // is still usable afterwards). Users are recommended for errors in both places
    // to distinguish between the error types.
    if (self.listenerCount('error') === 0) {
      self.client.emit('error', err)
    } else {
      self.emit('error', err)
    }
  }

We code:

const WebTorrent = require('webtorrent');

const client = new WebTorrent();

const torrentId = 'not-found';

client.add(torrentId, function (torrent) {

});

client.on('error', function (err) {
  console.error(err);
});

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: Invalid torrent identifier
    at ReadFileContext.callback (/project/node_modules/parse-torrent/index.js:78:26)
    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:359:13)

If there is a parsing error, We were unable to locate which torrent error.

So, we need provide torrentId argument.

if (err) {
  // Torrent errors are emitted at `torrent.on('error')`. If there are no 'error' event
  // handlers on the torrent instance, the error will be emitted at
  // `client.on('error')`. This prevents crashing the user's program, but it makes it
  // impossible to determine a client error versus a torrent error (where the client
  // is still usable afterwards). Users are recommended for errors in both places
  // to distinguish between the error types.
  if (self.listenerCount('error') != 0) {
    self.emit('error', err, torrentId);
  } else if (self.client.listenerCount('error') != 0) {
    self.client.emit('error', err, torrentId);
  } else {
    process.emit('error', err, torrentId);
  }
}
@starzou

This comment has been minimized.

Copy link
Author

@starzou starzou commented Jun 16, 2016

@feross what do you think?

@feross

This comment has been minimized.

Copy link
Member

@feross feross commented Jul 27, 2016

If there is a parsing error, you can locate which torrent is responsible using torrent.on('error')

var torrent1 = client.add(torrentId1, function (torrent) {
});

torrent1.on('error', function (err) {
  console.error('torrent1 error', err);
});

var torrent2 = client.add(torrentId2, function (torrent) {
});

torrent2.on('error', function (err) {
  console.error('torrent2 error', err);
});

The client.add call actually returns the torrent object right away, even though it's also passed into the callback.

@feross feross closed this Jul 27, 2016
@feross feross added the question label Jul 27, 2016
@lock

This comment has been minimized.

Copy link

@lock lock bot commented May 4, 2018

This thread has been automatically locked because it has not had recent activity. To discuss futher, please open a new issue.

@lock lock bot locked as resolved and limited conversation to collaborators May 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.