86 lines
2.4 KiB
Python
86 lines
2.4 KiB
Python
from dataclasses import dataclass
|
|
|
|
from common.utils import compute_hash
|
|
from core.BuiltinConcepts import BuiltinConcepts
|
|
from core.ExecutionContext import ExecutionContext
|
|
|
|
|
|
class SheerkaException(Exception):
|
|
def get_error_msg(self) -> str:
|
|
pass
|
|
|
|
|
|
class MethodAccessError(SheerkaException):
|
|
def __init__(self, method_name):
|
|
self.method_name = method_name
|
|
|
|
def get_error_msg(self) -> str:
|
|
return f"Cannot access method '{self.method_name}'"
|
|
|
|
|
|
class NotEnoughParameters(SheerkaException):
|
|
"""
|
|
Exception when not enough parameters are found during Sya parsing
|
|
"""
|
|
|
|
def __init__(self, concept_to_recognize, expected_nb_parameters, nb_parameters_found):
|
|
self.concept = concept_to_recognize
|
|
self.expected = expected_nb_parameters
|
|
self.found = nb_parameters_found
|
|
|
|
def get_error_msg(self) -> str:
|
|
return f"Failed to parse {self.concept}. Expecting {self.expected} parameters, but only found {self.found}."
|
|
|
|
|
|
@dataclass
|
|
class ErrorObj:
|
|
def get_error_msg(self) -> str:
|
|
pass
|
|
|
|
|
|
class ErrorContext(ErrorObj):
|
|
"""
|
|
This class represents the result of a data flow processing
|
|
"""
|
|
|
|
def __init__(self, who: str, context: ExecutionContext, value: object = None):
|
|
self.who = who
|
|
self.context = context
|
|
self.value = value
|
|
self.parents = None
|
|
|
|
def __repr__(self):
|
|
return f"Error(who={self.who}, context_id={self.context.medium_id}, value={self.value})"
|
|
|
|
def __eq__(self, other):
|
|
if id(self) == id(other):
|
|
return True
|
|
|
|
if not isinstance(other, ErrorContext):
|
|
return False
|
|
|
|
return self.who == other.who and \
|
|
self.context.id == other.context.id and \
|
|
self.value == other.value
|
|
|
|
def __hash__(self):
|
|
return hash((self.who, self.context.id, compute_hash(self.value)))
|
|
|
|
def get_error_msg(self):
|
|
value_as_list = self.value if isinstance(self.value, list) else [self.value]
|
|
temp = []
|
|
for value in value_as_list:
|
|
if isinstance(value, str):
|
|
temp.append(value)
|
|
elif isinstance(value, (SheerkaException, ErrorObj)):
|
|
temp.append(value.get_error_msg())
|
|
else:
|
|
temp.append(repr(value))
|
|
|
|
return ", ".join(temp)
|
|
|
|
|
|
ErrorConcepts = {BuiltinConcepts.UNKNOWN_CONCEPT,
|
|
BuiltinConcepts.EVALUATION_ERROR,
|
|
BuiltinConcepts.INVALID_CONCEPT}
|