socket.io-client icon indicating copy to clipboard operation
socket.io-client copied to clipboard

Server Middleware returning next(new Error(.......)) is not sent to the client

Open MrDesjardins opened this issue 2 years ago • 0 comments

Describe the bug

I have a middleware that returns an Error object in some particular situation. I see the error in the server's console but I do not see the error being sent to the client.

Server:"socket.io": "^4.5.0" Client: "socket.io-client": "^4.5.0"

To Reproduce

Socket.IO server version: x4.5.0

Server

import { Event, Socket } from "socket.io";
import { ServiceLayer } from "../services/serviceLayer";
export const userActivateMiddleware =
  (serviceLayer: ServiceLayer, socket: Socket) =>
  async (event: Event, next: (err?: Error) => void) => {
    const user = socket.data.user;

    if (user === undefined) {
      return next(new Error("User Email Not validated"));
    } 
    return next();
  };

// Elsewhere:
socket.use(userActivateMiddleware(serviceLayer, socket));

Socket.IO client version: 4.5.0

Client

import { io } from "socket.io-client";

// Listeners
socket.on("connect_error", socketConnectError);
socket.on("error", socketError);

function socketError(err: any): void {
    console.log("Socket Error", err);
}
function socketConnectError(error: Error): void {
    console.log("socketConnectError", error);
}

// How the request starts
socket.emit(MsgUserPixelKind, message, (response: MsgUserPixelValidation) => {
    manageResponseFromMsgUserPixel(response);
});

Expected behavior The emit is performed and the NodeJS Socket.io servers receive the request. I can set a breakpoint and see the request getting inside the middleware. In the case I am testing, a rejection, I see the code getting into the if and executing the return next(new Error("....."));. I am expected to get the client to be notified somehow (by the connect_error or error or by the callback. However, nothing.

Platform:

  • Device: Macbook Pro
  • OS: MacOS 12.1

Additional context Looking at the Chrome's network ws tab, I see the request sent which collaborate that I can have the NodeJS server hit my breakpoint. However, no response. The callback is not called either.

MrDesjardins avatar May 20 '22 03:05 MrDesjardins