代码之家  ›  专栏  ›  技术社区  ›  Estus Flask

蓝鸟忘记返回警告丢失

  •  5
  • Estus Flask  · 技术社区  · 7 年前

    我期待蓝鸟 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));
    

    如何修复输出警告?

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

    3 回复  |  直到 7 年前
        1
  •  4
  •   Andrew Ault    7 年前

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

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

    或环境变量( docs ( demo ):

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

    BLUEBIRD_LONG_STACK_TRACES=1 BLUEBIRD_WARNINGS=1 node app.js
    

    如果蓝鸟调试,这两个功能都会自动启用。 环境变量已设置,或者如果节点环境 变量等于“发展”。

    要在节点开发中启用长堆栈跟踪和警告,请执行以下操作:

    $ NODE_ENV=development node server.js
    

    要在节点生产中启用长堆栈跟踪和警告,请执行以下操作:

    $ BLUEBIRD_DEBUG=1 node server.js
    

    Environment Variables .


    编辑为什么需要这样做:

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

    请注意,尽管这里的默认值是false,但是可能会检测到一个自动启用长堆栈跟踪和警告的开发环境。

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

        2
  •  2
  •   Adrian Pop    7 年前

    您可以配置用于检查忘记的返回语句的警告,并在 wForgottenReturn longStackTraces 配置对象的属性。 WFORGOTTRETURN公司 是的财产 warning 并且必须设置为true,并且是唯一可以单独配置的警告类型。相应的环境变量键是 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) 以启用它们。如果没有这个选项,蓝鸟的警告就会少很多。 有用的。如果没有调用堆栈,就很难弄清楚 发出警告的地方。

    还可以找到更多信息:

        3
  •  1
  •   antzshrek    7 年前

    用一句话回答你的问题,

    如何修复输出警告?

    通过使能 长堆栈跟踪 .

    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.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;
    };
    

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


    下降到 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(Object {
        warnings: boolean=false,
        longStackTraces: boolean=false,
        cancellation: boolean=false,
        monitoring: boolean=false
    } options) -> Object;
    

    配置长堆栈跟踪、警告、监视和取消。注意,尽管如此 如果是这里的默认值,则可能会检测到一个自动启用长堆栈跟踪和警告的开发环境。

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

    WFORGOTTRETURN公司 是唯一可以单独配置的警告类型。相应的环境变量键是 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
    

    你可能想检查一下这个 official source 来自蓝鸟的贡献者之一

    推荐文章