dbusjs / node-dbus-next

🚌 The next great dbus library for node
https://www.npmjs.com/package/dbus-next
155 stars 52 forks source link

Logging errors from event handlers #102

Open zdila opened 2 years ago

zdila commented 2 years ago

Hello,

First thanks for the great library.

I have a question about how to make errors thrown from handlers to be (at least) logged.

For example:

properties.on('PropertiesChanged', (iface, changed, invalidated) => {
  throw new Error('ew'); // this is not loged anywhere
});

Thanks.

zdila commented 2 years ago

Maybe errors should be re-thrown like this:

try {
  handler(params);
} catch (err) {
  process.nextTick(() => {
    throw err;
  });
}
zdila commented 2 years ago

My workaround:

function propagateHandlerError<T extends any[]>(
  fn: (...params: T) => void
) {
  return (...params: T) => {
    try {
      fn(...params);
    } catch (err) {
      process.nextTick(() => {
        throw err;
      });
    }
  };
}

properties.on('PropertiesChanged', propagateHandlerError((iface, changed, invalidated) => {
  throw new Error('ew');
}));