From 035f69919dd057bd43ba9826c519b82b7c4efbb7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 26 Jan 2013 22:46:47 -0500 Subject: [PATCH] better error reporting. add test --- index.js | 21 +++++++++++++++------ test/test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 5d671d2..7b6a181 100644 --- a/index.js +++ b/index.js @@ -143,14 +143,23 @@ function createServer(options) { user: client.username, serverId: digest }); - request.end(function (err,resp){ + request.end(function(err, resp) { + var myErr; if (err) { - client.end('Error :', err); - return; - } - if (resp.text !== "YES") { + server.emit('error', err); + client.end('InternalError'); + } else if (resp.serverError) { + myErr = new Error("session.minecraft.net is broken: " + resp.status); + myErr.code = 'EMCSESSION500'; + server.emit('error', myErr); + client.end('McSessionDown'); + } else if (resp.serverError) { + myErr = new Error("session.minecraft.net rejected request: " + resp.status); + myErr.code = 'EMCSESSION400'; + server.emit('error', myErr); + client.end('McSessionRejectedAuthRequest'); + } else if (resp.text !== "YES") { client.end('FailedToVerifyUsername'); - return; } }); } diff --git a/test/test.js b/test/test.js index 9b401b3..9a13e5a 100644 --- a/test/test.js +++ b/test/test.js @@ -525,6 +525,37 @@ describe("mc-server", function() { } } }); + it("kicks clients when invalid credentials", function(done) { + var server = mc.createServer(); + var count = 5; + server.on('connection', function(client) { + client.on('end', function(reason) { + resolve(); + server.close(); + }); + }); + server.on('close', function() { + resolve(); + }); + server.on('listening', function() { + resolve(); + var client = mc.createClient({ + username: 'lalalal', + password: "this is wrong", + }); + client.on('error', function(err) { + assert.strictEqual(err.code, 'ELOGIN400'); + resolve(); + }); + client.on('end', function() { + resolve(); + }); + }); + function resolve() { + count -= 1; + if (count <= 0) done(); + } + }); it("gives correct reason for kicking clients when shutting down", function(done) { var server = mc.createServer({ 'online-mode': false, }); var count = 2;