32 lines
823 B
Python
32 lines
823 B
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
from typing import Any
|
|
|
|
from core.errors import (
|
|
DeviceNotFoundError,
|
|
DeviceOfflineError,
|
|
ElementNotFoundError,
|
|
DriverError,
|
|
)
|
|
|
|
|
|
def semantic_error(exc: Exception) -> str:
|
|
if isinstance(exc, DeviceOfflineError):
|
|
return "device offline"
|
|
if isinstance(exc, DeviceNotFoundError):
|
|
return "device not found"
|
|
if isinstance(exc, ElementNotFoundError):
|
|
return "element not found"
|
|
if isinstance(exc, DriverError):
|
|
return "driver error"
|
|
return "operation failed"
|
|
|
|
|
|
def call_with_semantic_errors(func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
|
|
try:
|
|
return func(*args, **kwargs)
|
|
except Exception as exc:
|
|
return {"ok": False, "error": semantic_error(exc)}
|
|
|