Socket.io: Throwing error in middleware does not bubble up event to UI (client socket io)

Created on 26 Oct 2018  路  1Comment  路  Source: socketio/socket.io

  • [x] report a bug

Current behaviour

I have some middleware I wrote to throw a new error when a JWT isn't present. I have placed a throw new Errror directly inside the middleware, so it always throws an error.

The error never arrives on the client (using the client api from socket-io).

The UI - I am listening to all possible events and they seem to work but I get NO error or any event when i throw the new Error on the node layer.

Steps to reproduce (if the current behaviour is a bug)

Here is the code on the node layer

    this.socketServer = socketIO(this.httpServer)

    this.socketServer.use((socket, next) => {
      // var token = socket.handshake.query.auth_token
      return next(new Error("Authentication error"))
    })

and here is the ui

  this.socket = new io.Manager(this.configService.getConfig().serverUri as any, {
      query: "auth_token=THE_JWT_TOKEN"
    })

    this.socket.on("connect_error", (a: any, b: any, c: any) => {
      debugger
    })

    this.socket.on("disconnect", (err: any) => {
      debugger
    })

    this.socket.on("reconnect", (a: any, b: any, c: any) => {
      debugger
    })

    this.socket.on("reconnect_attempt", (a: any, b: any, c: any) => {
      debugger
    })

    this.socket.on("reconnecting", (a: any, b: any, c: any) => {
      this.socket.opts.query = "auth_token=CHANGE_JWT"
    })

    this.socket.on("error", (a: any, b: any, c: any) => {
      debugger
    })

Expected behaviour

Throwing an error inside the socket-io middleware in node should trigger some kind of event inside the UI - so that I am able to respond with it ... But it does not.

Setup

  • OS: Mac mojave
  • browser: chrome
  • socket.io version:

    "socket.io": "2.1.1",
    "socket.io-client": "2.1.1",

>All comments

You are only creating an error, not throwing it ...
Use throw, do not use next()

Was this page helpful?
0 / 5 - 0 ratings