javascript - 如何在用作 Dialogflow Webhook 的 Google Cloud Function 中实现 Promise 拒绝选项?

标签 javascript node.js google-cloud-functions dialogflow-es request-promise

我正在构建一个 Dialogflow 聊天机器人,它使用 Google Cloud Function 作为 Webhook,并使用 Node.js 和 request-promise-native,并且不使用 Firebase 或 Google Actions。我的 webhook 可以很好地从外部 API 获取和返回我想要的数据(Promise 已解决),但现在我在尝试使用 Promise 拒绝编写错误处理代码时遇到问题。

这是一段代码,显示了我正在尝试执行的操作。如果故意错误的 URL,代码会跳转到 .catch block ,而不会进入 Promise拒绝选项,并且云函数日志会显示错误消息“Unhandledrejection”。

function searchMyData(agent) {
    return new Promise((resolve, reject) => {
        var myUrl = 'deliberately wrong to trigger rejection';

        // Make the HTTP request with request-promise-native
        // https://www.npmjs.com/package/request-promise

        var options = {
            uri: myUrl,
            headers: {
                'User-Agent': 'Request-Promise-Native'
            },
            json: true
        };

        rpn(options)
            .then((json) => {
                if(json) {
                    // Whole bunch of code for getting the desired data 
                    // and resolving the Promise
                    // This part works
                    var result = agent.add('Here is your data');
                    resolve(result); // Promise resolved
                }
                else { // This block is not run, why?
                    // Reject Promise and return error message to Dialogflow
                    console.log('Promise rejected');
                    var rejectMessage = 'Sorry, an error occurred.';
                    agent.add(rejectMessage);
                    reject(rejectMessage);
                }
            }) // .then end
            .catch((err) => { 
                console.log('In .catch block'); // This is run
                throw new Error('err.message from .catch: '+ err.message); // This is run
            }); // .catch end
    }); // Promise end
} // searchMyData end

我不清楚如何构造代码以使其在出现错误时运行 Promise 拒绝。这里使用的结构是我在教程中看到的,Promise 在 if block 中解析,在 else block 中拒绝,全部在 .then 内,然后是 .catch。但我的代码从未到达 else block 。

或者,是否可以完全忽略 Promise 被拒绝的选项,或者它会在某处产生隐藏的问题吗?万一 webhook 不起作用,我真正想做的就是在 Dialogflow 中向用户返回错误消息(并在内部记录错误)。

最佳答案

阅读 Promise。被拒绝的 Promise 将作为 then block 中的第二个参数返回。

rpn(options)
        .then((json) => {
            if(json) {
                // Whole bunch of code for getting the desired data 
                // and resolving the Promise
                // This part works
                var result = agent.add('Here is your data');
                resolve(result); // Promise resolved
            }
}, error => { // This block will be run on promise rejection

                console.log('Promise rejected');
                var rejectMessage = 'Sorry, an error occurred.';
                agent.add(rejectMessage);
                reject(rejectMessage);
            }
        }) // .then end

关于javascript - 如何在用作 Dialogflow Webhook 的 Google Cloud Function 中实现 Promise 拒绝选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55608935/

相关文章:

javascript - 具有多个输入的自定义 Knockout 绑定(bind),其中一个包含索引数组

node.js - 使用默认快捷方式运行多个任务

node.js - 从另一个 docker 容器中访问在 docker 容器中运行的服务

node.js - Google Cloud 上的 GCS 功能比本地主机慢

javascript - trim 小数点前的数字

javascript - Perl 得到延迟的 javascript 响应

javascript - 从嵌套循环 Javascript 中的一个循环访问参数

Node.js Soap 附件文件/cid href

javascript - OnCreate 函数未触发,但 OnWrite 函数触发

firebase - 错误 : 16 UNAUTHENTICATED: Failed to retrieve auth metadata with error: Could not refresh access token