【问题标题】:How to use addErrorHandlers in Alexa ASK SDK V2如何在 Alexa ASK SDK V2 中使用 addErrorHandlers
【发布时间】: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


    【解决方案1】:

    Someone on the Alexa forums helped me find my issue.

    基本上,我的 CustomErrorHandler 不正确地访问了 deviceId 并引发了错误。将CustomErrorHandler 中的行更改为此后,问题得到解决,错误处理程序按预期工作。

    const deviceId = handlerInput.requestEnvelope.context.System.device.deviceId;
    

    而不是

    const deviceId = handlerInput.context.System.device.deviceId;
    

    另一种可能是好的做法是有一个备份错误处理程序来捕获错误处理程序中的错误,该错误处理程序只是打印出错误以避免备份处理程序中出现任何问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 2019-05-04
      • 1970-01-01
      • 2019-07-11
      • 2020-06-26
      • 1970-01-01
      相关资源
      最近更新 更多