onPossiblyUnhandledRejection sets some global state in the module instance to record the function to call in the event of an unhandled rejection.
However, since npm's default is to install a separate instance of modules in the "node_modules" directory of modules that the app depends on, it's difficult for the app to set up error handling for instances of Bluebird which are referenced from another module.
I came across this with Sequelize - I set up an error handler at the top level in my app, and it usually works fine. However, sometimes the default error handler is used instead. It turned out that this is due to the separate instance of Bluebird that Sequelize is using.
(In fact, certain versions of Sequelize use the sequelize-bluebird module instead, which has some differences, but the same problem applies)
The problem gets worse as your app uses more modules that make use of Bluebird.
I don't really have a good suggested solution, but it seems like this is the sort of thing that an app wants to configure for the entire system, rather than a specific instance of the module.
The only ways that I can really think of to do this is for Bluebird to use some global object in Node.js to co-ordinate the error handling mechanism across the different Bluebird instances. The most likely candidate is the 'process' object, I guess.
For example, this could be done by recording the error handling function in a property of the process object, which would be shared by all Bluebird instances, or by emitting an event on the process object instead of using the onPossiblyUnhandledRejection mechanism, which the app could listen for.
I realise that npm installs modules this way to isolate modules from behavioural changes in their dependencies when other modules in the system have difference versions of the module, but in this case I think it might be worth putting in a 'global' mechanism to set the error handler.
onPossiblyUnhandledRejection sets some global state in the module instance to record the function to call in the event of an unhandled rejection.
However, since npm's default is to install a separate instance of modules in the "node_modules" directory of modules that the app depends on, it's difficult for the app to set up error handling for instances of Bluebird which are referenced from another module.
I came across this with Sequelize - I set up an error handler at the top level in my app, and it usually works fine. However, sometimes the default error handler is used instead. It turned out that this is due to the separate instance of Bluebird that Sequelize is using.
(In fact, certain versions of Sequelize use the sequelize-bluebird module instead, which has some differences, but the same problem applies)
The problem gets worse as your app uses more modules that make use of Bluebird.
I don't really have a good suggested solution, but it seems like this is the sort of thing that an app wants to configure for the entire system, rather than a specific instance of the module.
The only ways that I can really think of to do this is for Bluebird to use some global object in Node.js to co-ordinate the error handling mechanism across the different Bluebird instances. The most likely candidate is the 'process' object, I guess.
For example, this could be done by recording the error handling function in a property of the process object, which would be shared by all Bluebird instances, or by emitting an event on the process object instead of using the onPossiblyUnhandledRejection mechanism, which the app could listen for.
I realise that npm installs modules this way to isolate modules from behavioural changes in their dependencies when other modules in the system have difference versions of the module, but in this case I think it might be worth putting in a 'global' mechanism to set the error handler.