Module: Sxn::MCP::Tools::BaseTool::ErrorMapping

Defined in:
lib/sxn/mcp/tools/base_tool.rb

Overview

Maps sxn errors to user-friendly messages

Constant Summary collapse

SXN_ERROR_MESSAGES =
{
  # Session errors
  Sxn::SessionNotFoundError => "Session not found",
  Sxn::SessionAlreadyExistsError => "Session already exists",
  Sxn::SessionExistsError => "Session already exists",
  Sxn::SessionHasChangesError => "Session has uncommitted changes",
  Sxn::InvalidSessionNameError => "Invalid session name",
  Sxn::NoActiveSessionError => "No active session",
  # Project errors
  Sxn::ProjectNotFoundError => "Project not found",
  Sxn::ProjectAlreadyExistsError => "Project already exists",
  Sxn::ProjectExistsError => "Project already exists",
  Sxn::ProjectInUseError => "Project is in use",
  Sxn::InvalidProjectNameError => "Invalid project name",
  Sxn::InvalidProjectPathError => "Invalid project path",
  # Worktree errors
  Sxn::WorktreeError => "Worktree error",
  Sxn::WorktreeExistsError => "Worktree already exists",
  Sxn::WorktreeNotFoundError => "Worktree not found",
  Sxn::WorktreeCreationError => "Failed to create worktree",
  Sxn::WorktreeRemovalError => "Failed to remove worktree",
  # Template errors
  Sxn::SessionTemplateNotFoundError => "Template not found",
  Sxn::SessionTemplateValidationError => "Template validation failed",
  # Rule errors
  Sxn::RuleError => "Rule error",
  Sxn::RuleNotFoundError => "Rule not found",
  Sxn::InvalidRuleTypeError => "Invalid rule type",
  Sxn::InvalidRuleConfigError => "Invalid rule configuration",
  # Configuration errors
  Sxn::ConfigurationError => "Configuration error",
  # MCP errors
  Sxn::MCPError => "MCP error",
  Sxn::MCPServerError => "MCP server error",
  Sxn::MCPValidationError => "MCP validation error"
}.freeze

Class Method Summary collapse

Class Method Details

.error_response(message) ⇒ Object



59
60
61
# File 'lib/sxn/mcp/tools/base_tool.rb', line 59

def self.error_response(message)
  ::MCP::Tool::Response.new([{ type: "text", text: message }], error: true)
end

.wrapObject

Wrap a block with error handling that converts sxn errors to error responses



48
49
50
51
52
53
54
55
56
57
# File 'lib/sxn/mcp/tools/base_tool.rb', line 48

def self.wrap
  yield
rescue Sxn::ConfigurationError => e
  error_response("sxn not initialized: #{e.message}. Run 'sxn init' first.")
rescue Sxn::Error => e
  error_type = SXN_ERROR_MESSAGES[e.class] || "Error"
  error_response("#{error_type}: #{e.message}")
rescue StandardError => e
  error_response("Unexpected error: #{e.message}")
end