Quantcast
Channel: User Arman P. - Stack Overflow
Viewing all articles
Browse latest Browse all 36

Answer by Arman P. for Socket.io server "Error-handling middleware" equivalent of express?

$
0
0

You can't have error handling middleware for socket.io as in express, but you can still do that a little bit differently, just by wrapping the callback in your own error handler, like:

const errorHandler = (handler) => {  const handleError = (err) => {    console.error("please handle me", err);  };  return (...args) => {    try {      const ret = handler.apply(this, args);      if (ret && typeof ret.catch === "function") {        // async handler        ret.catch(handleError);      }    } catch (e) {      // sync handler      handleError(e);    }  };};// server or client sidesocket.on("hello", errorHandler(() => {  throw new Error("let's panic");}));

Example is taken from the official documentation


Viewing all articles
Browse latest Browse all 36

Latest Images

Trending Articles





Latest Images