from .apptypes import RenderingResult


class RendererError(Exception):
    """Generic rendering error"""

    def __init__(self, message: str, details: str) -> None:
        super().__init__(message + details)
        self.short = message
        self.details = details

    def response(self) -> RenderingResult:
        """Get a response object out of the error"""
        return {
            "runDetails": None,
            "error": {
                "errorString": self.short,
                "errorDetail": self.details,
            },
        }


class MissingTemplateString(RendererError):
    """Special error for a missing 'templateString'"""

    def __init__(self) -> None:
        message = "Request must contain `userContext` and `templateString`"
        super().__init__(message, message)


class WrongTypeError(RendererError):
    """Special error for a type checking failure"""

    def __init__(self, key: str, expected: str, got: str) -> None:
        message = f'Invalid request: Expected "{key}" to be a {expected}, got "{got}"'
        super().__init__(message, message)
