From 61cfe0afa6cb58be7c2dff71ec01ed11e7f9ffe4 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 18 Aug 2014 01:40:30 -0700 Subject: [PATCH] don't emit 'error' for non-fatal errors --- README.md | 6 +++++- client.js | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 62ae6bf..63a657e 100644 --- a/README.md +++ b/README.md @@ -45,10 +45,14 @@ var port = 6881 var client = new Client(peerId, port, parsedTorrent) -// you must add an 'error' event handler! client.on('error', function (err) { + // fatal client error! console.log(err.message) +}) + +client.on('warning', function (err) { // a tracker was unavailable or sent bad data to the client. you can probably ignore it + console.log(err.message) }) // start getting peers from the tracker diff --git a/client.js b/client.js index d0b40c8..1416d47 100644 --- a/client.js +++ b/client.js @@ -388,7 +388,8 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) { } function error (message) { - self.client.emit('error', new Error(message + ' (connecting to tracker ' + requestUrl + ')')) + // errors will often happen if a tracker is offline, so don't treat it as fatal + self.client.emit('warning', new Error(message + ' (' + requestUrl + ')')) cleanup() } @@ -443,7 +444,7 @@ Tracker.prototype._handleResponse = function (requestUrl, data) { try { data = bencode.decode(data) } catch (err) { - return self.client.emit('error', new Error('Error decoding tracker response: ' + err.message)) + return self.client.emit('warning', new Error('Error decoding tracker response: ' + err.message)) } var failure = data['failure reason'] if (failure) {