我正试图从V1中提升我的技能,以询问SDK V2。我在StandardSkillBuilder上使用StandardSkillBuilder函数有困难。当我在我的意图中抛出错误时,我的自定义错误处理程序永远不会被调用。我不知道怎么用。
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从来没有添加任何响应。
发布于 2019-02-05 18:38:49
基本上,我的CustomErrorHandler是不正确地访问deviceId并本身抛出一个错误。在将CustomErrorHandler中的行更改为此之后,问题得到了解决,错误处理程序按预期工作。
const deviceId = handlerInput.requestEnvelope.context.System.device.deviceId;而不是
const deviceId = handlerInput.context.System.device.deviceId;其他一些可能是好的做法是有一个备份错误处理程序来捕获错误处理程序中的错误,这些错误处理程序只需打印出错误,以避免备份处理程序中出现任何错误。
https://stackoverflow.com/questions/54526599
复制相似问题