Skip to content

Commit

Permalink
Handle empty error. Closes hapijs#156
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Mar 20, 2017
1 parent e155548 commit ef81dda
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@
update.statusCode >= 400 &&
update.statusCode <= 599) {

error = new NesError(update.payload.message || update.payload.error, 'server');
error = new NesError(update.payload.message || update.payload.error || 'Error', 'server');
error.statusCode = update.statusCode;
error.data = update.payload;
error.headers = update.headers;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nes",
"description": "WebSocket adapter plugin for hapi routes",
"version": "6.4.1",
"version": "6.4.2",
"repository": "git://github.com/hapijs/nes",
"main": "lib/index.js",
"browser": "dist/client.js",
Expand Down
36 changes: 36 additions & 0 deletions test/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,42 @@ describe('Socket', () => {
});
});

it('sends errors from callback (code)', (done) => {

const onMessage = function (socket, message, reply) {

expect(message).to.equal('winning');
const error = Boom.badRequest();
error.output.payload = {};
reply(error);
};

const server = new Hapi.Server();
server.connection();
server.register({ register: Nes, options: { onMessage } }, (err) => {

expect(err).to.not.exist();

server.start((err) => {

expect(err).to.not.exist();
const client = new Nes.Client('http://localhost:' + server.info.port);
client.connect(() => {

client.message('winning', (err, response) => {

expect(err).to.exist();
expect(err.message).to.equal('Error');
expect(err.statusCode).to.equal(400);
expect(response).to.not.exist();
client.disconnect();
server.stop(done);
});
});
});
});
});

it('errors if missing onMessage callback', (done) => {

const server = new Hapi.Server();
Expand Down

0 comments on commit ef81dda

Please sign in to comment.