Socket.io-client: Middleware error not catches

Created on 5 Jun 2014  ·  8Comments  ·  Source: socketio/socket.io-client

I might be making a mistake, but I cannot manage to catch middleware error on client, here is server code:

(function () {
    'use strict';

    var url = require("url"),
        sio = require('socket.io')(3010);

    //Authentification required
    sio.use(function (socket, next) {
        return next(new Error('refused client'));
    });
}());

and client code:

(function () {
    "use strict";

    //Network
    var socket = io.connect(SOCKET_IO_SERVER);

    socket.on('error', function(){
        console.error(arguments);
    });
}());

So is it a bug ?

EDIT: Using network tab on chrome I do see a polling request containing "refused client"

Most helpful comment

I have the same issue when using namespaces. @oliveiragabriel07 solution worked for me as well but I think the client should be able to receive an error thrown in the general connection middelware ( io.use ) I guess this is a socket.io server issue

All 8 comments

No one?

We can't run your example. The better thing to do would be to provide a failing test or a repo with an example we can just run to see the failure. There are too many other factors here that could influence this.

What steps have you taken to debug your issue?

+1
Error callback is not beeing called on client side when Error is passed as parameter to next on middleware.

Edit

I found that when using namespaces, the middleware should be attached to the namespace:

var nsp = io.of('/namespace');

// client never receives the error event
io.use(function (socket, next) {
    return next(new Error('refused client')); 
});

// this way works
nsp.use(function (socket, next) {
    return next(new Error('refused client')); 
});

@oliveiragabriel07 thanks, that seems to be the exact issue, it client is actually refused, but doesn't receive the error.

I created a small repo with a working exception https://github.com/Korri/socket.io-issue688

I'm able to catch middleware errors in the client in socket.io 1.0 w/the 'error' event handler.

I have the same issue when using namespaces. @oliveiragabriel07 solution worked for me as well but I think the client should be able to receive an error thrown in the general connection middelware ( io.use ) I guess this is a socket.io server issue

I agree with @jbaez.

to catch error you need return after next(). i think this is bug in express.js too.
you should have like this:

io.use(function (socket, next) {
       next(new Error('refused client')); 
       return;
});

this is not good

io.use(function (socket, next) {
       return next(new Error('refused client'));
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

najibghadri picture najibghadri  ·  7Comments

crazyyi picture crazyyi  ·  5Comments

BorntraegerMarc picture BorntraegerMarc  ·  4Comments

david-fong picture david-fong  ·  4Comments

catamphetamine picture catamphetamine  ·  3Comments