# retoor <retoor@molodetz.nl>

from __future__ import annotations

import json
from typing import Any


class DeviiError(Exception):
    code: str = "error"

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

    def as_payload(self) -> dict[str, Any]:
        payload: dict[str, Any] = {"error": self.code, "message": self.message}
        payload.update(self.details)
        return payload


class AuthRequiredError(DeviiError):
    code = "auth_required"


class NetworkError(DeviiError):
    code = "network_error"


class UpstreamError(DeviiError):
    code = "upstream_error"


class ToolInputError(DeviiError):
    code = "tool_input_error"


class LLMError(DeviiError):
    code = "llm_error"


def error_result(error: DeviiError) -> str:
    return json.dumps(error.as_payload(), ensure_ascii=False)


def unexpected_result(exc: Exception) -> str:
    return json.dumps(
        {"error": "unexpected", "message": str(exc), "type": type(exc).__name__},
        ensure_ascii=False,
    )
