官术网_书友最值得收藏!

External APIs

Every time you deal with anything outside of your code, there is a risk that it will error. This might be a third-party API, your own API, or simply a query to a database. You should always write these requests so that if the request returns an error, you fully deal with it. This means logging what the error was and where it took place and making sure that the chatbot still works when an error occurs.

Making sure that the chatbot still works when an error occurs is really important as no one wants to talk to a chatbot that just stops talking to you halfway through the conversation. To make sure this doesn't happen, you have three options: create error messages for every external call you make, let all errors flow down to a very low-level error handler that sends a generic We had an error message, or a combination of the two. The idea would be using custom messages for every error that could happen but as your chatbot becomes larger and more complicated, that can become very time-consuming.

An effective method for dealing with the errors is to create a low-level error handler that passes a generic error message unless a specific error message is provided. This gives you the flexibility to let the user know exactly what went wrong when it matters but saves you having to create lots of similar error messages:

try {
let result = AccessPeopleAPI();
if (result === null || typeof result !== 'number'){
throw 'I've failed to get the number of people';
}
return 'We have ' + result + ' people in the building';
} catch (error) {
console.log(error || 'The system has had an error');
return error || "Unfortunately I've had an error";
}
主站蜘蛛池模板: 凭祥市| 神池县| 胶州市| 华蓥市| 绵阳市| 乐业县| 堆龙德庆县| 资溪县| 海兴县| 大名县| 万山特区| 洛宁县| 达拉特旗| 乐亭县| 开封市| 江达县| 敖汉旗| 南充市| 富阳市| 上思县| 青冈县| 望城县| 裕民县| 揭东县| 伊宁县| 大港区| 莆田市| 大理市| 广安市| 平昌县| 秦安县| 土默特右旗| 老河口市| 三亚市| 荣昌县| 佛教| 甘南县| 嵊泗县| 都兰县| 博湖县| 沙湾县|