【问题标题】:Bluebird forgotten return warning is missing蓝鸟忘记返回警告丢失
【发布时间】:2023-03-28 10:45:01
【问题描述】:

我预计 Bluebird forgotten return warning 会出现,但由于某种原因它不起作用。

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));

如何修复输出警告?

我怀疑我之前已经遇到过这个问题,但我不记得解决方案是什么。

【问题讨论】:

  • 你试过不同版本的蓝鸟吗?
  • @Dimitar 我正在使用最新版本 3.5.3(可以在演示中看到)。我希望那里会出现警告。

标签: javascript node.js bluebird


【解决方案1】:

似乎需要启用长堆栈跟踪才能显示警告。您可以使用配置对象来启用它们 (docs) (demo):

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

或者环境变量(docs)(demo):

在 Node.js 中,您可以为 整个过程使用环境变量:

BLUEBIRD_LONG_STACK_TRACES=1 BLUEBIRD_WARNINGS=1 node app.js

如果 BLUEBIRD_DEBUG 环境变量已设置,或者如果 NODE_ENV 环境 变量等于“发展”。

在节点开发中启用长堆栈跟踪和警告:

$ NODE_ENV=development node server.js

在节点生产中启用长堆栈跟踪和警告:

$ BLUEBIRD_DEBUG=1 node server.js

Environment Variables


编辑为什么这是必要的:

似乎警告和长堆栈跟踪都默认禁用,只有在检测到开发环境时才启用,请参阅here

请注意,即使此处默认设置为 false,也可能会检测到自动启用长堆栈跟踪和警告的开发环境。

要在生产环境中显示警告,您不仅必须启用警告,还必须启用长堆栈跟踪,请参阅herehere

【讨论】:

  • 谢谢。这就是解决方案。我一直在使用长堆栈跟踪,所以问题从未发生过。非常欢迎解释(最好来自官方来源)为什么需要长堆栈跟踪才能使被遗忘的返回警告起作用,因为这似乎不是不言而喻的事情。
  • This page 是我能在上面找到的最官方的文档:“请注意,即使此处默认设置为 false,也可能会检测到自动启用长堆栈跟踪和警告的开发环境。”此外,this comment 表明这种行为是故意的,this commit 表明这种情况一直存在。
  • 另请参阅@Adrian Pop 的评论以获取特定于堆栈跟踪的信息(尽管该标志似乎不会影响未显示的警告)。
  • 是的,刚刚测试过,但它对节点 9.x 没有影响。也许这只是一个 6.x+ 的问题。
【解决方案2】:

您可以使用配置对象中的wForgottenReturnlongStackTraces 属性之间的组合来配置检查忘记的返回语句的警告。 wForgottenReturnwarning 的属性,必须设置为true,是唯一可以单独配置的警告类型。对应的环境变量key为BLUEBIRD_W_FORGOTTEN_RETURN。您可以查看documentation 了解更多信息。

const Bluebird = require('bluebird');

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


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

在控制台中运行程序给了我:

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

这是你想要的输出。

您还可以将应用程序运行为:

BLUEBIRD_LONG_STACK_TRACES=1 BLUEBIRD_WARNINGS=1 node app.js,产生相同的结果。

干杯!

编辑:

来自this github 上的问题,我们有:

所以问题是默认情况下 Nodejs 6.x 不显示堆栈 警告的痕迹。有一个命令行选项(--trace-warnings) 启用它们。如果没有这个选项,蓝鸟警告会少很多 有用。没有调用堆栈,很难弄清楚 警告的来源。

还可以找到更多信息:

【讨论】:

  • 谢谢。这就是解决方案。我一直在使用长堆栈跟踪,所以问题从未发生过。非常欢迎解释(最好来自官方来源)为什么需要长堆栈跟踪才能使被遗忘的返回警告起作用,因为这似乎不是不言而喻的事情。
  • @estus 请参阅this github 关于 Bluebird 的问题并且没有返回警告。这实际上是一个节点问题,而不是一个蓝鸟问题。我还用其他链接更新了我的答案,所以现在你有了一个“官方”答案:) 干杯!
【解决方案3】:

一句话回答你的问题,

如何修复输出警告?

通过启用长堆栈跟踪

const Bluebird = require('bluebird');

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

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

这应该会让你最终得到一个new demo,它会标记你这个错误:

(node:65) Warning: a promise was created in a handler at evalmachine.<anonymous>:16:14 but was not returned from it, see http://bluebirdjs.com/docs/warning-explanations.html#warning-a-promise-was-created-in-a-handler-but-was-not-returned-from-it
    at Function.Promise.cast (/home/runner/node_modules/bluebird/js/release/promise.js:196:13)
undefined

promise.js 文件:


Promise.cast = function (obj) {
    var ret = tryConvertToPromise(obj);
    if (!(ret instanceof Promise)) {
        ret = new Promise(INTERNAL);
        ret._captureStackTrace(); //promise.js:196:13 
        ret._setFulfilled();
        ret._rejectionHandler0 = obj;
    }
    return ret;
};

请记住,在 Node.js 中,您可以选择使用环境变量为整个进程配置警告和长堆栈跟踪。


进入bluebird的源代码


var warnings = !!(util.env("BLUEBIRD_WARNINGS") != 0 &&
    (debugging || util.env("BLUEBIRD_WARNINGS")));

var longStackTraces = !!(util.env("BLUEBIRD_LONG_STACK_TRACES") != 0 &&
    (debugging || util.env("BLUEBIRD_LONG_STACK_TRACES")));

var wForgottenReturn = util.env("BLUEBIRD_W_FORGOTTEN_RETURN") != 0 &&
    (warnings || !!util.env("BLUEBIRD_W_FORGOTTEN_RETURN"));

Promise.config = function(opts) {
    opts = Object(opts);
    if ("longStackTraces" in opts) {
        if (opts.longStackTraces) {
            Promise.longStackTraces();
        } else if (!opts.longStackTraces && Promise.hasLongStackTraces()) {
            disableLongStackTraces();
        }
    }
    if ("warnings" in opts) {
        var warningsOption = opts.warnings;
        config.warnings = !!warningsOption;
        wForgottenReturn = config.warnings;

        if (util.isObject(warningsOption)) {
            if ("wForgottenReturn" in warningsOption) {
                wForgottenReturn = !!warningsOption.wForgottenReturn;
            }
        }
    }
    if ("cancellation" in opts && opts.cancellation && !config.cancellation) {
        if (async.haveItemsQueued()) {
            throw new Error(
                "cannot enable cancellation after promises are in use");
        }
        Promise.prototype._clearCancellationData =
            cancellationClearCancellationData;
        Promise.prototype._propagateFrom = cancellationPropagateFrom;
        Promise.prototype._onCancel = cancellationOnCancel;
        Promise.prototype._setOnCancel = cancellationSetOnCancel;
        Promise.prototype._attachCancellationCallback =
            cancellationAttachCancellationCallback;
        Promise.prototype._execute = cancellationExecute;
        propagateFromFunction = cancellationPropagateFrom;
        config.cancellation = true;
    }
    if ("monitoring" in opts) {
        if (opts.monitoring && !config.monitoring) {
            config.monitoring = true;
            Promise.prototype._fireEvent = activeFireEvent;
        } else if (!opts.monitoring && config.monitoring) {
            config.monitoring = false;
            Promise.prototype._fireEvent = defaultFireEvent;
        }
    }
    return Promise;
};

默认情况下,长堆栈跟踪、警告、监控和取消都通过将它们放在生产环境中false 来禁用。在开发环境中打开调试器时,它们会被检测到并自动启用。

我会建议你再去bluebird's documentation 一次。

Promise.config

Promise.config(Object {
    warnings: boolean=false,
    longStackTraces: boolean=false,
    cancellation: boolean=false,
    monitoring: boolean=false
} options) -> Object;

配置长堆栈跟踪、警告、监控和取消。请注意,即使 false 是此处的默认值,也可能会检测到自动启用长堆栈跟踪和警告的开发环境。

Promise.config({
    // Enable warnings
    warnings: true,
    // Enable long stack traces
    longStackTraces: true,
    // Enable cancellation
    cancellation: true,
    // Enable monitoring
    monitoring: true
});

您可以使用wForgottenReturn配置检查忘记返回语句的警告:

Promise.config({
    // Enables all warnings except forgotten return statements.
    warnings: {
        wForgottenReturn: false
    }
});

wForgottenReturn 是唯一可以单独配置的警告类型。对应的环境变量key为BLUEBIRD_W_FORGOTTEN_RETURN


在 Node.js 中,您可以使用环境变量为整个进程配置警告和长堆栈跟踪:

BLUEBIRD_LONG_STACK_TRACES=1 BLUEBIRD_WARNINGS=1 node app.js

如果设置了BLUEBIRD_DEBUG 环境变量或者NODE_ENV 环境变量等于"development",则这两个功能都会自动启用。

使用值0 将显式禁用某个功能,尽管调试环境会激活它:

# Warnings are disabled despite being in development environment
NODE_ENV=development BLUEBIRD_WARNINGS=0 node app.js

您可能想从 bluebird 的一位贡献者那里查看official source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多