"""Custom exceptions for Swifty ViewSets."""

from typing import Any
from rest_framework.status import HTTP_400_BAD_REQUEST


class JsonException(Exception):
    """Exception that renders as a JSON response.

    Use this in viewsets to return structured JSON error responses
    with custom status codes.

    Example:
        raise JsonException({"field": "Invalid value"}, status_code=422)
    """

    def __init__(self, message: Any, status_code: int = HTTP_400_BAD_REQUEST) -> None:
        self.status_code = status_code
        self.message = (
            {"errors": str(message)}
            if not isinstance(message, (dict, list))
            else message
        )
        super().__init__(self.message)
