"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.AlexaErrorResponse = void 0; const crypto_1 = require("crypto"); const build_js_1 = require("../messages/build.js"); const errors_js_1 = require("../messages/errors.js"); const topics = __importStar(require("../topics.js")); const transport_js_1 = require("../transport.js"); /** An ErrorResponse as 1.x builds it: device.getErrorMessage(token), setErrorMessage(), send(). */ class AlexaErrorResponse { constructor(correlationToken, rootTopic, endpointId, publisher) { this.correlationToken = correlationToken; this.rootTopic = rootTopic; this.endpointId = endpointId; this.publisher = publisher; // Sent when setErrorMessage() was not called: 1.x published an empty payload, which is no ErrorResponse to Alexa this.error = errors_js_1.AlexaErrors.of("INTERNAL_ERROR", "the device answered with an error and did not say which"); // One id for the message, however often toJSON() is called this.messageId = (0, crypto_1.randomUUID)(); } /** * The error to answer with. otherParams are the fields the type adds to the payload (validRange). The header * carries the namespace the type is documented under, Alexa.ThermostatController for THERMOSTAT_IS_OFF; * options.namespace names another. */ setErrorMessage(type, message, otherParams = {}, options = {}) { this.error = errors_js_1.AlexaErrors.of(type, message, otherParams); this.namespace = options.namespace; } toJSON() { const { type, alexaMessage, extra } = this.error; return (0, build_js_1.errorResponse)({ endpointId: this.endpointId, messageId: this.messageId, correlationToken: this.correlationToken, type, message: alexaMessage, extra, namespace: this.namespace ?? this.error.namespace, }); } /** Resolves with the topic published to, or "" when the publish failed. Never rejects. */ send(sendAsync = false) { const answer = sendAsync ? topics.deferred : topics.response; return (0, transport_js_1.send)(this.publisher, answer(this.rootTopic, this.endpointId), () => this.toJSON(), this.onPublishError); } } exports.AlexaErrorResponse = AlexaErrorResponse;