Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upError handling #1473
Error handling #1473
Comments
This comment has been minimized.
This comment has been minimized.
|
A note on this, if you do think any of this is a good idea, I would be willing to look into making an appropriate PR to make these adjustments. The larger impact I think would be it would change the API (though it could be done by adding a method instead of changing the existing one). |
This comment has been minimized.
This comment has been minimized.
|
The way to do this with the current API is like this const client = new WebTorrent()
client.add(torrentId, torrent => {
torrent.on('error', err => {
// handle errors for this specific torrent here
})
})There are plans for a promise based API in the future. The reason this isn't an error in the Multiple errors can happen in the same torrent or client as well. |
This comment has been minimized.
This comment has been minimized.
|
The callback isn't called if there is an error parsing (or adding in case of duplicates). |
This comment has been minimized.
This comment has been minimized.
|
Ah, I see the issue. The callback for If you only register the error handler in the callback, then any errors that happen before the torrent is You can register the error handler immediately using the return value of const client = new WebTorrent()
const torrent = client.add('invalid torrent id')
torrent.on('error', error => {
console.log('error caught!') //handle errors here
})Here's a jsfiddle with this code https://jsfiddle.net/9tks8vw4/1/ |
This comment has been minimized.
This comment has been minimized.
|
This works in only very strictly limited cases and is very difficult to manage. If I, for example, have 20 torrents I would like to add to a given client. When I add each one, I would need to proceed to the next one only after either an error or after the Again, I would be willing to modify or update the existing API in a PR to add a error callback. Looking at the code though... everything seems very tightly coupled with event emitting and there seems to be no error handling of any given function from the caller and instead they are handled inside and have a hidden dependency on a "global" error callback. I think any change would involve moving all event emitting to something which calls the functions instead of inside the functions themselves. I am not sure a PR would get through with such heavy modifications. Also, I think I would need to change all functions to take a success/failure callback parameter so that each step is individual and doesn't know about the next step. After that, they should no longer have a dependency on event emitting and could be changed to promises relatively easily. |
This comment has been minimized.
This comment has been minimized.
I don't see why this is the case. There's a separate handler for torrent errors Have you seen https://webtorrent.io/docs? Search for
Since torrent errors are fatal, I don't think the error handler would be called multiple times, but if so, then you can use |
This comment has been minimized.
This comment has been minimized.
|
Errors aren't just emitted in response to function calls. There are lots of on going processes like network sockets, interactions with peers, and file system access that can all experience errors but don't really have a callback to send them to. |
This comment has been minimized.
This comment has been minimized.
I already stated this is a concern for before the
This wouldn't work very well. If there is an error all error handlers would be called for the What I would specifically like to handle properly is errors during
I do not see why this should not be the case with |
This comment has been minimized.
This comment has been minimized.
|
Here is a simple example of what I am talking about: I used |
This comment has been minimized.
This comment has been minimized.
|
Here's the fix https://jsfiddle.net/9tks8vw4/9/ |
This comment has been minimized.
This comment has been minimized.
|
Hmmm... ok I obviously didn't look at the return type when looking at the code. Also, it looks like this is in the documentation but not shown in any examples or anything which may have been the source of my confusion From docs:
I think it would be good to at least annotate the return type. Maybe an example with error handling on add would be helpful (it seems like someone everyone would need initially using this as a library). I apologize for any confusion =/. |
This comment has been minimized.
This comment has been minimized.
|
Happy to help! It's not intuitive you're right. You could submit a PR for the docs if you wanted. https://github.com/webtorrent/webtorrent/tree/master/docs Error handling would be a good addition to https://github.com/webtorrent/webtorrent/blob/master/docs/get-started.md I think |
What version of WebTorrent?
0.102.1
What operating system and Node.js version?
10.7.0
What browser and version? (if using WebTorrent in the browser)
n/a
What did you expect to happen?
Able to handle an error and properly correlate it with event that initiated async task.
What actually happened?
No way to efficiently correlate errors with initial call.
This is much more of a feature request. I would like to hear your thoughts on though. I understand the need for having a "global" catching mechanism for some cases. It seems built like an application instead of a library I can use easily.
For example, if I have several torrents I want to start downloading, I want to add several "torrents". When doing this, there is no way to easily correlate which error events correspond to which call to "add". In doing so, it makes it extremely difficult to manage. It seems to me, it would be far easier to manage if you used the fairly standard "function(err, successResult)" callback signature back to the callback so the caller can receive/handle error appropriately. Better yet, instead of taking a callback simply return a promise which can be rejected if there is an error during the
addprocess specifically (not during the torrent downloading process).