【发布时间】:2019-02-05 01:16:55
【问题描述】:
我正在尝试将我的技能从 V1 升级到 ASK SDK V2。我在使用StandardSkillBuilder 上的addErrorHandlers 函数时遇到问题。当我在意图中抛出错误时,我的自定义错误处理程序永远不会被调用。不知道怎么用。
index.ts
import { SkillBuilders } from 'ask-sdk';
import { LambdaHandler } from 'ask-sdk-core/dist/skill/factory/BaseSkillFactory';
// import intents
import LaunchIntent from './alexa-intents/launch.intent';
import CustomErrorHandler from './alexa-intents/custom-error-handler';
function buildAlexaLambdaHandler(): LambdaHandler {
return SkillBuilders.standard()
.addRequestHandlers(
new LaunchIntent()
)
.addErrorHandlers(new CustomErrorHandler())
.lambda();
}
export const handler = buildAlexaLambdaHandler();
launch.intent.ts(抛出测试错误)
import { HandlerInput, RequestHandler } from 'ask-sdk';
import { Response } from 'ask-sdk-model'
export default class LaunchIntent implements RequestHandler {
canHandle(handlerInput: HandlerInput): boolean {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest';
}
handle(handlerInput: HandlerInput): Response {
const responseBuilder = handlerInput.responseBuilder;
const deviceId = handlerInput.requestEnvelope.context.System.device.deviceId;
if (deviceId) {
console.log('throwing error');
throw new Error('test error');
}
return responseBuilder
.speak('welcome')
.getResponse();
}
}
custom-error-handler.ts(尝试捕获测试错误)
import { HandlerInput, ErrorHandler } from 'ask-sdk';
import { Response } from 'ask-sdk-model';
export default class CustomErrorHandler implements ErrorHandler {
canHandle(handlerInput: HandlerInput): boolean {
return true;
}
handle(handlerInput: HandlerInput, error: Error): Response {
const request = handlerInput.requestEnvelope.request;
const deviceId = handlerInput.context.System.device.deviceId;
console.dir(error);
return handlerInput.responseBuilder
.speak('there was an error')
.getResponse();
}
}
我正在使用 bespoken-tools LambdaServer 在本地运行我的技能:
import * as bst from 'bespoken-tools';
const server = new bst.LambdaServer('./src/index', 10000, true);
server.start(() => console.log('[init.dev]: server started and listening on port 10000!'));
在我的控制台日志中,我看到了来自启动意图处理程序“抛出错误”的控制台消息,但是我从未看到调用的 CustomErrorHandler 或其中的任何控制台日志。在 Alexa 开发人员控制台模拟器中,我收到“请求的技能响应有问题”,因为 CustomerErrorHandler 从未添加任何响应。
【问题讨论】:
标签: node.js alexa alexa-skills-kit alexa-skill