"""Error types and helpers for MCP tool responses."""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
@dataclass
class SonarQubeError(Exception):
"""Structured error for tool operations."""
code: str
message: str
details: dict[str, Any] = field(default_factory=dict)
def __str__(self) -> str:
return f"{self.code}: {self.message}"
def error_response(error: Exception) -> dict[str, Any]:
"""Return a consistent error envelope for tools."""
if isinstance(error, SonarQubeError):
return {
"ok": False,
"error_code": error.code,
"message": error.message,
"details": error.details,
}
return {
"ok": False,
"error_code": "internal_error",
"message": str(error),
"details": {},
}