Result Error Chain Ledger
This commit is contained in:
parent
958c906f30
commit
b6275269b5
6 changed files with 101 additions and 16 deletions
|
|
@ -49,6 +49,21 @@ def connected_session_profile_ids() -> list|None:
|
||||||
return state_repo.global_state.session_profile_ids
|
return state_repo.global_state.session_profile_ids
|
||||||
|
|
||||||
|
|
||||||
|
def get_all_connected() -> list|None:
|
||||||
|
profile_ids = connected_session_profile_ids()
|
||||||
|
system = connected_system_profile_id()
|
||||||
|
if system:
|
||||||
|
profile_ids.append(system)
|
||||||
|
return profile_ids
|
||||||
|
|
||||||
|
def quantity_connected() -> list|None:
|
||||||
|
connected = get_all_connected()
|
||||||
|
if connected:
|
||||||
|
return len(connected)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def is_connected(profile_id: int) -> bool:
|
def is_connected(profile_id: int) -> bool:
|
||||||
"""Returns a boolean if a given profile is connected"""
|
"""Returns a boolean if a given profile is connected"""
|
||||||
|
|
||||||
|
|
@ -80,7 +95,6 @@ def coordinate_evaluation(connection_observer: ConnectionObserver) -> Result:
|
||||||
|
|
||||||
logger.info("Extracted JSON state data")
|
logger.info("Extracted JSON state data")
|
||||||
|
|
||||||
|
|
||||||
# full profile:
|
# full profile:
|
||||||
profile = ProfileController.get(profile_id)
|
profile = ProfileController.get(profile_id)
|
||||||
if not profile:
|
if not profile:
|
||||||
|
|
@ -189,10 +203,10 @@ def evaluate_state_on_startup() -> Result:
|
||||||
#-----------------------
|
#-----------------------
|
||||||
# SYSTEM-WIDE
|
# SYSTEM-WIDE
|
||||||
#-----------------------
|
#-----------------------
|
||||||
systemwide_active = SystemStateController.get()
|
systemwide_state = SystemStateController.get()
|
||||||
if systemwide_active:
|
if systemwide_state:
|
||||||
error_msg = "The previous system-wide connection was not correctly turned off before the app was killed. Do you want to evaluate it? (no blindly kills it)"
|
error_msg = "The previous system-wide connection was not correctly turned off before the app was killed. Do you want to evaluate it? (no blindly kills it)"
|
||||||
no_option = partial(kill_any_protocol, current_state=systemwide_active, active_protocol=systemwide_active.protocol)
|
no_option = partial(kill_any_protocol, current_state=systemwide_state, active_protocol=systemwide_state.protocol)
|
||||||
return Result(valid=False, no_option=no_option, error_type=ResultError.ZOMBIE_CONNECTION, message=error_msg)
|
return Result(valid=False, no_option=no_option, error_type=ResultError.ZOMBIE_CONNECTION, message=error_msg)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
from core.infrastructure.plumbing_tools.result_chain import unwrap, audit
|
||||||
from core.infrastructure.thread_dispatch import MainThreadDispatcher
|
from core.infrastructure.thread_dispatch import MainThreadDispatcher
|
||||||
from core.infrastructure.error_popup import PopupProtocol, StackErrorProtocol
|
from core.infrastructure.error_popup import PopupProtocol, StackErrorProtocol
|
||||||
from core.infrastructure.service_registry import ServiceLocator
|
from core.infrastructure.service_registry import ServiceLocator
|
||||||
|
|
@ -55,15 +56,24 @@ class ThreadPlumbing:
|
||||||
logger.error(error_msg)
|
logger.error(error_msg)
|
||||||
result = Result(valid=False, error_stack=error_stack, error_type=ResultError.UNKNOWN, message=error_msg)
|
result = Result(valid=False, error_stack=error_stack, error_type=ResultError.UNKNOWN, message=error_msg)
|
||||||
finally:
|
finally:
|
||||||
self._on_complete(result) # Process the result
|
# Process the result:
|
||||||
|
self._on_complete(original_result=result)
|
||||||
|
|
||||||
# 3. Spin up a single thread and start it
|
# 3. Spin up a single thread and start it
|
||||||
thread = threading.Thread(target=wrapper, daemon=True)
|
thread = threading.Thread(target=wrapper, daemon=True)
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
def _on_complete(self, result: Result):
|
def _on_complete(self, original_result: Result):
|
||||||
"""Get a potential function prescription and return it to the main thread."""
|
"""Get a potential function prescription and return it to the main thread."""
|
||||||
|
|
||||||
|
#----------------------------
|
||||||
|
# Wrapper:
|
||||||
|
#----------------------------
|
||||||
|
if not original_result.valid:
|
||||||
|
logger.error(audit(original_result))
|
||||||
|
|
||||||
|
result = unwrap(original_result)
|
||||||
|
|
||||||
#----------------------------
|
#----------------------------
|
||||||
# Went Right with a New Function Prescription to Run Right Now:
|
# Went Right with a New Function Prescription to Run Right Now:
|
||||||
#----------------------------
|
#----------------------------
|
||||||
|
|
|
||||||
1
core/infrastructure/plumbing_result_chain.py
Normal file
1
core/infrastructure/plumbing_result_chain.py
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
|
||||||
41
core/infrastructure/plumbing_tools/result_chain.py
Normal file
41
core/infrastructure/plumbing_tools/result_chain.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
from core.models.Result import Result
|
||||||
|
|
||||||
|
def unwrap(result: Result) -> Result:
|
||||||
|
"""Returns the original root Result that started the chain."""
|
||||||
|
current = result
|
||||||
|
while current.caused_by:
|
||||||
|
current = current.caused_by
|
||||||
|
return current
|
||||||
|
|
||||||
|
def audit(result: Result) -> str:
|
||||||
|
"""Generates a detailed ledger string for the logger"""
|
||||||
|
chain = []
|
||||||
|
current = result
|
||||||
|
|
||||||
|
# Traverse the chain to collect all events
|
||||||
|
while current:
|
||||||
|
# We only care about the message if it differs from the child's message
|
||||||
|
msg = None
|
||||||
|
if current.caused_by:
|
||||||
|
if current.message != current.caused_by.message:
|
||||||
|
msg = current.message
|
||||||
|
|
||||||
|
chain.append({
|
||||||
|
"func": current.created_by,
|
||||||
|
"msg": msg
|
||||||
|
})
|
||||||
|
current = current.caused_by
|
||||||
|
|
||||||
|
# Reverse the chain so it reads from Original -> Final
|
||||||
|
chain.reverse()
|
||||||
|
|
||||||
|
lines = []
|
||||||
|
for i, entry in enumerate(chain):
|
||||||
|
prefix = "Original creator" if i == 0 else f"Caller {i}"
|
||||||
|
line = f"{prefix}: {entry['func']}"
|
||||||
|
if entry['msg']:
|
||||||
|
line += f" | added message: \"{entry['msg']}\""
|
||||||
|
lines.append(line)
|
||||||
|
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
@ -2,9 +2,10 @@ from core.services.networking.api_requests.ApiResponseModel import BackoffStrate
|
||||||
from core.errors.ErrorStack import ErrorStack
|
from core.errors.ErrorStack import ErrorStack
|
||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass, field
|
||||||
from typing import Optional, Any
|
from typing import Optional, Any
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
import inspect
|
||||||
|
|
||||||
class ResultError(Enum):
|
class ResultError(Enum):
|
||||||
"""Classified error categories."""
|
"""Classified error categories."""
|
||||||
|
|
@ -77,11 +78,29 @@ class Result():
|
||||||
no_option: Optional[partial] = None
|
no_option: Optional[partial] = None
|
||||||
double_dip: bool = False
|
double_dip: bool = False
|
||||||
navigate_to: Optional[str] = None
|
navigate_to: Optional[str] = None
|
||||||
|
created_by: Optional[str] = None
|
||||||
|
caused_by: Optional['Result'] = None
|
||||||
|
created_by: Optional[str] = field(default=None, init=False)
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
# def user_message(self) -> str:
|
|
||||||
# """Human-readable error for the UI."""
|
|
||||||
# messages = {
|
|
||||||
# ResultError.SUCCESS: "Operation completed successfully.",
|
|
||||||
# ResultError.UNKNOWN: f"Error: {self.message}",
|
|
||||||
# }
|
|
||||||
# return messages.get(self.error_type, "Unknown error")
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue