UNPKG

@kajidog/mcp-tts-voicevox

Version:

VOICEVOX integration for MCP - Text-to-Speech server using VOICEVOX engine

116 lines (115 loc) 4.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorHandler = exports.VoicevoxError = exports.VoicevoxErrorCode = void 0; exports.handleError = handleError; exports.formatError = formatError; /** * エラーハンドリングを行い、適切なエラーメッセージとともに例外をスローします * @param message エラーメッセージのプレフィックス * @param error 発生したエラー * @returns never(常に例外をスロー) */ function handleError(message, error) { const errorMsg = error instanceof Error ? error.message : String(error); console.error(`${message}: ${errorMsg}`, error); throw new Error(`${message}: ${errorMsg}`); } /** * エラーハンドリングを行い、エラーメッセージを返します (例外をスローしない) * @param message エラーメッセージのプレフィックス * @param error 発生したエラー * @returns エラーメッセージ */ function formatError(message, error) { const errorMsg = error instanceof Error ? error.message : String(error); console.error(`${message}: ${errorMsg}`, error); return `${message}: ${errorMsg}`; } /** * VOICEVOX関連のエラーコード */ var VoicevoxErrorCode; (function (VoicevoxErrorCode) { VoicevoxErrorCode["API_CONNECTION_ERROR"] = "api_connection_error"; VoicevoxErrorCode["QUERY_GENERATION_ERROR"] = "query_generation_error"; VoicevoxErrorCode["SYNTHESIS_ERROR"] = "synthesis_error"; VoicevoxErrorCode["FILE_OPERATION_ERROR"] = "file_operation_error"; VoicevoxErrorCode["PLAYBACK_ERROR"] = "playback_error"; VoicevoxErrorCode["QUEUE_OPERATION_ERROR"] = "queue_operation_error"; VoicevoxErrorCode["UNKNOWN_ERROR"] = "unknown_error"; })(VoicevoxErrorCode || (exports.VoicevoxErrorCode = VoicevoxErrorCode = {})); /** * VOICEVOXエラークラス */ class VoicevoxError extends Error { constructor(message, code = VoicevoxErrorCode.UNKNOWN_ERROR, originalError) { super(message); this.name = "VoicevoxError"; this.code = code; this.originalError = originalError; } /** * エラー発生箇所とスタックトレースを含むエラーの詳細情報を取得 */ getDetailedMessage() { let details = `${this.message} [${this.code}]`; if (this.originalError instanceof Error) { details += `\nOriginal Error: ${this.originalError.message}`; if (this.originalError.stack) { details += `\nStack: ${this.originalError.stack}`; } } return details; } } exports.VoicevoxError = VoicevoxError; /** * エラーハンドリングユーティリティクラス * アプリケーション全体で統一されたエラーハンドリングを提供 */ class ErrorHandler { /** * エラーをVoicevoxError形式に変換して例外をスロー */ static throw(message, code = VoicevoxErrorCode.UNKNOWN_ERROR, originalError) { console.error(`[${code}] ${message}`, originalError); throw new VoicevoxError(message, code, originalError); } /** * APIエラーを処理 */ static handleApiError(message, error) { return this.throw(message, VoicevoxErrorCode.API_CONNECTION_ERROR, error); } /** * クエリ生成エラーを処理 */ static handleQueryGenerationError(message, error) { return this.throw(message, VoicevoxErrorCode.QUERY_GENERATION_ERROR, error); } /** * 音声合成エラーを処理 */ static handleSynthesisError(message, error) { return this.throw(message, VoicevoxErrorCode.SYNTHESIS_ERROR, error); } /** * ファイル操作エラーを処理 */ static handleFileError(message, error) { return this.throw(message, VoicevoxErrorCode.FILE_OPERATION_ERROR, error); } /** * 再生エラーを処理 */ static handlePlaybackError(message, error) { return this.throw(message, VoicevoxErrorCode.PLAYBACK_ERROR, error); } /** * キュー操作エラーを処理 */ static handleQueueError(message, error) { return this.throw(message, VoicevoxErrorCode.QUEUE_OPERATION_ERROR, error); } } exports.ErrorHandler = ErrorHandler;