如何在用作 Dialogflow webhook 的 Google Cloud Function 中达到 Promise 拒绝选项?
How to reach Promise reject option in a Google Cloud Function used as a Dialogflow webhook?
我正在构建一个 Dialogflow 聊天机器人,它使用 Google Cloud Function 作为 webhook,具有 node.js 和 request-promise-native 并且不使用 Firebase 或 Google Actions。我的 webhook 可以很好地从外部 API 获取和 returning 我想要的数据(Promise 已解决),但现在我在尝试使用 Promise reject 编写错误处理代码时遇到了问题。
这是一段代码,展示了我正在尝试做的事情。如果故意错误 URL,代码会跳转到 .catch 块,而不会转到 Promise 拒绝选项,并且云函数日志会显示错误消息 "Unhandled rejection".
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 块中解决并在 else 块中拒绝,所有内容都在 .then 中,然后是 .catch。但是我的代码永远不会到达 else 块。
或者,是否可以完全省略 Promise rejected 选项,或者它会在某处产生隐藏的问题?如果 webhook 不起作用,我真正想做的是 return 在 Dialogflow 中向用户发送一条错误消息(并在内部记录错误)。
阅读 Promises。被拒绝的承诺作为 then 块中的第二个参数返回。
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
我正在构建一个 Dialogflow 聊天机器人,它使用 Google Cloud Function 作为 webhook,具有 node.js 和 request-promise-native 并且不使用 Firebase 或 Google Actions。我的 webhook 可以很好地从外部 API 获取和 returning 我想要的数据(Promise 已解决),但现在我在尝试使用 Promise reject 编写错误处理代码时遇到了问题。
这是一段代码,展示了我正在尝试做的事情。如果故意错误 URL,代码会跳转到 .catch 块,而不会转到 Promise 拒绝选项,并且云函数日志会显示错误消息 "Unhandled rejection".
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 块中解决并在 else 块中拒绝,所有内容都在 .then 中,然后是 .catch。但是我的代码永远不会到达 else 块。
或者,是否可以完全省略 Promise rejected 选项,或者它会在某处产生隐藏的问题?如果 webhook 不起作用,我真正想做的是 return 在 Dialogflow 中向用户发送一条错误消息(并在内部记录错误)。
阅读 Promises。被拒绝的承诺作为 then 块中的第二个参数返回。
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