29 lines
798 B
Python
29 lines
798 B
Python
|
|
|
|
from enum import Enum
|
|
from dataclasses import dataclass
|
|
from typing import Optional, Any
|
|
|
|
class ResultError(Enum):
|
|
"""Classified error categories."""
|
|
SUCCESS = "success"
|
|
NOT_SUPPORTED = "NOT_SUPPORTED"
|
|
INVALID_INPUT = "invalid_input"
|
|
CONNECTION = "connection"
|
|
DATABASE = "database"
|
|
UNKNOWN = "unknown"
|
|
|
|
@dataclass
|
|
class Result:
|
|
valid: bool
|
|
error_type: ResultError = ResultError.SUCCESS
|
|
data: Optional[Any] = None
|
|
message: Optional[str] = None
|
|
|
|
def user_message(self) -> str:
|
|
"""Human-readable error for the UI."""
|
|
messages = {
|
|
DBErrorType.SUCCESS: "Operation completed successfully.",
|
|
DBErrorType.UNKNOWN: f"Error: {self.message}",
|
|
}
|
|
return messages.get(self.error_type, "Unknown error")
|