from core.services.networking.api_requests.ApiResponseModel import BackoffStrategy from core.errors.ErrorStack import ErrorStack from enum import Enum from dataclasses import dataclass, field from typing import Optional, Any from functools import partial import inspect class ResultError(Enum): """Classified error categories.""" SUCCESS = "success" NOT_SUPPORTED = "NOT_SUPPORTED" INVALID_INPUT = "invalid_input" REVERT_VERSION = "revert_version" DELETE_DB = "delete_db" MISSING_FILE = "missing_file" MISSING_DEPENDENCY = "missing_dependency" MISSING_DATA = "missing_data" MISSING_SOFTWARE = "missing_software" SETUP_SINGBOX = "setup_singbox" FILE_SYSTEM = "filesystem" CONNECTION = "connection" TOR = "tor" DATABASE = "database" PERMISSION = "permission" SUBSCRIPTION = "subscription" EXPIRED_INVOICE = "expired_invoice" # meant for invoice UNPAID = "unpaid" PAID = "paid" # this is in-case it was expected to be unpaid EXPIRED_SUB = "expired_sub" ACTIVE_BILLING = "active_billing" BILLING_CONFLICT = "billing_conflict" STATE_CONFLICT = "state_conflict" # with actions to kill STATE = "state" ZOMBIE_SYSTEM = "zombie_system" ZOMBIE_SESSION = "zombie_session" TICKET = "ticket" NO_TICKET = "no_ticket" RANDOM_TICKETS_OFF = "random_tickets_off" TICKET_SETUP = "ticket_setup" TICKET_EXPIRED = "ticket_expired" ALREADY_EXISTS = "already_exists" ALREADY_USED = "already_used" BILLING_CODE_EXISTS = "billing_code_exists" PROCESS_GOT_KILLED = "process_got_killed" PROCESS_WONT_START = "process_wont_start" PROCESS_MISMATCH = "process_mismatch" NMCLI = "nmcli_issues" FIREWALL = "firewall" CLIENT_DNS = "client_dns" EXTERNAL_DNS = "external_dns" INTERFACE = "interface" TIMEOUT = "timeout" INVALID_API_REPLY = "invalid_api_reply" ENCRYPTION = "encryption" ENDPOINT_VERIFICATION = "endpoint_verification" LEAK_ISSUE = "leak_issue" NEED_SYNC = "need_sync" WRONG_LOCATION = "wrong_location" SERVER_SIDE = "server_side" MTU = "mtu" TEST_MTU = "test_mtu" TOR_NEEDED = "tor_needed" API_DOWN = "api_down" NODE_ERROR = "node_error" CHANGE_LOCATION = "change_location" NO_INTERNET = "no_internet" PORT = "port" RENEGOTIATION = "renegotiation" ATTEMPTS = "attempts" RECONNECT = "reconnect" NAVIGATE_TO = "navigate_to" KILL_ENTIRE_APP = "kill_entire_app" UNKNOWN = "unknown" @dataclass class Result(): valid: bool goal_result: Optional[bool] = None error_type: ResultError = ResultError.SUCCESS error_stack: Optional[ErrorStack] = None data: Optional[Any] = None message: Optional[str] = None goal: Optional[str] = None backoff_strategy: Optional[BackoffStrategy] = None function_choice: Optional[partial] = None no_option: Optional[partial] = None double_dip: bool = False navigate_to: Optional[str] = None created_by: Optional[str] = None caused_by: Optional['Result'] = None created_by: Optional[str] = field(default=None, init=False) profile_id: Optional[int] = None def __post_init__(self): stack = inspect.stack() # stack[0] is __post_init__ # stack[1] is __init__ # stack[2] is the immediate caller caller_frame = stack[2] # If the immediate caller is the 'wrap' method, # jump back one more frame to find who called 'wrap'. if caller_frame.function == 'wrap': caller_frame = stack[3] object.__setattr__(self, 'created_by', caller_frame.function) def wrap(self, new_message: Optional[str] = None) -> 'Result': """Wraps this result in a new layer. Uses existing message if none provided.""" return Result( valid=False, message=new_message if new_message else self.message, caused_by=self )