Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluebird forgotten return warning is missing

I expect Bluebird forgotten return warning to appear but it doesn't work for some reason.

A demo:

const Bluebird = require('bluebird');

Bluebird.config({
    warnings: true
})

Bluebird.resolve(1)
.then(() => {
    Bluebird.resolve(2); // should warn about forgotten return
})
.then(two => console.log(two));

How can it be fixed to output a warning?

I suspect I already encountered this problem before but I don't remember what was the solution.

like image 607
Estus Flask Avatar asked Jan 19 '19 11:01

Estus Flask


2 Answers

It seems that long stack traces need to be enabled for the warnings to show. You can use the config object to enable them (docs) (demo):

Bluebird.config({
    warnings: true,
    longStackTraces: true
});

Or environment variables (docs) (demo):

In Node.js you may configure warnings and long stack traces for the entire process using environment variables:

BLUEBIRD_LONG_STACK_TRACES=1 BLUEBIRD_WARNINGS=1 node app.js

Both features are automatically enabled if the BLUEBIRD_DEBUG environment variable has been set or if the NODE_ENV environment variable is equal to "development".

and

To enable long stack traces and warnings in node development:

$ NODE_ENV=development node server.js

To enable long stack traces and warnings in node production:

$ BLUEBIRD_DEBUG=1 node server.js

See Environment Variables.


Edit as to why this is necessary:

It seems that both warnings and long stack traces are disabled by default, and are only enabled if a development environment is detected, see here:

Note that even though false is the default here, a development environment might be detected which automatically enables long stack traces and warnings.

To get warnings to show in a production environment, not only do you have to enable warnings, you have to enable long stack traces as well, see here and here.

like image 79
Andrew Ault Avatar answered Sep 28 '22 07:09

Andrew Ault


You can configure the warning for checking forgotten return statements with a combination between wForgottenReturn and longStackTraces properties from the config object. wForgottenReturn is a property of warning and must be set to true and is the only warning type that can be separately configured. The corresponding environmental variable key is BLUEBIRD_W_FORGOTTEN_RETURN. You may check the documentation for more info.

const Bluebird = require('bluebird');

Bluebird.config({
    warnings: {
        wForgottenReturn: true
    }, longStackTraces: true,
});


Bluebird.resolve(1).then(() => {
   Bluebird.resolve(2);
}).then(two => console.log(two));

Running the program in the console gives me:

Warning: a promise was created in a handler at /home/adrianpop/test/bb.js:11:13 but was not returned from it, see 
    at Function.Promise.cast (/home/adrianpop/Downloads/Test/node_modules/bluebird/js/release/promise.js:196:13)
undefined

which is the desired output by you.

You can also run the application as:

BLUEBIRD_LONG_STACK_TRACES=1 BLUEBIRD_WARNINGS=1 node app.js, producing the same result.

Cheers!

Edit:

From this issue on github, we have that:

So the problem is that by default Nodejs 6.x does not display stack traces for warnings. There is a command line option (--trace-warnings) to enable them. Without this option Bluebird warnings are a lot less useful. Without the call stack, it can be very difficult to figure out where the warning originated.

More info can also be found:

  • here (github issue)
  • this (github PR)

  • this SO question.

like image 26
Adrian Pop Avatar answered Sep 28 '22 09:09

Adrian Pop