"""GeoScored SDK error types."""


class GeoScoredError(Exception):
    """Raised when the GeoScored API returns an error response."""

    def __init__(
        self,
        status_code: int,
        error_type: str,
        code: str,
        message: str,
        param: str | None = None,
        request_id: str | None = None,
    ):
        self.status_code = status_code
        self.error_type = error_type
        self.code = code
        self.message = message
        self.param = param
        self.request_id = request_id
        super().__init__(f"[{status_code}] {code}: {message}")

    @classmethod
    def from_response(cls, status_code: int, body: dict) -> "GeoScoredError":
        """Create from an API error response body."""
        error = body.get("error", {})
        return cls(
            status_code=status_code,
            error_type=error.get("type", "unknown"),
            code=error.get("code", "unknown"),
            message=error.get("message", "Unknown error"),
            param=error.get("param"),
            request_id=error.get("request_id"),
        )
