typed socket.io error handler
the challenge#
This is a error wrapper as described here: socket.io - error handling
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 side
socket.on(
"hello",
errorHandler(() => {
throw new Error("let's panic");
})
);
preparation#
We need fully typed events.