diff --git a/src/caosdb/exceptions.py b/src/caosdb/exceptions.py index 68f5e5eac68d0804075ac2e8f602dcac86848334..874e62f5ce3d0727fccc272a6731df785f0b29f1 100644 --- a/src/caosdb/exceptions.py +++ b/src/caosdb/exceptions.py @@ -133,45 +133,27 @@ class ResourceNotFoundException(CaosDBException): CaosDBException.__init__(self, msg=msg) -class TransactionError(CaosDBException): - - def _calc_bases(self): - types = dict() - # collect each class once - - for err in self.errors: - types[id(type(err))] = type(err) - # delete redundant super classes - - if len(types.values()) > 1: - # remove TransactionError - try: - del types[id(TransactionError)] - except KeyError: - pass +######################### Transaction errors ######################### - if len(types.values()) > 1: - # remove EntityError - try: - del types[id(EntityError)] - except KeyError: - pass - ret = () - - for t in types.values(): - ret += (t,) - - if ret == (): - ret = (type(self),) +class TransactionError(CaosDBException): + """An error of this type is raised whenever any transaction fails with + one or more entities between client and CaosDB server. More + detailed errors are collected as direct and indirect children in + the 'errors' list (direct children) and the 'all_errors' set (set + of all direct and indirect children). - return ret + """ - def __init__(self, container=None, error=None, msg=None): - self.container = container + def __init__(self, error=None, msg=None): self.errors = [] - self.msg = msg if msg is not None else str(error) - self.error = error + self.all_errors = set() + self.entities = [] + self.all_entities = set() + self.msg = msg + if error is not none: + self.add_error(error) + def print_errs(self): print(self) @@ -179,29 +161,33 @@ class TransactionError(CaosDBException): for err in self.errors: err.print_errs() - def _convert(self): - t = self._calc_bases() - try: - newtype = type('TransactionError', t, {}) - except BaseException: - self.print_errs() - raise - newinstance = newtype(container=self.container, error=self.msg) - newinstance.errors = self.errors - newinstance.get_entities = self.get_entities - - return newinstance - - def get_container(self): - ''' - @return: The container that raised this TransactionError during the last - transaction. - ''' + def has_error(self, error_t, direct_children_only=False): + """Check whether this transaction error contains an error of type + error_t. If direct_children_only is True, only direct children + are checked. + + Parameters: + ----------- + error_t : EntityError + error type to be checked + direct_children_only: bool, optional + If True, only direct children, i.e., all errors in + self.errors are checked. Else all direct and indirect + children, i.e., all errors in self.all_errors are + used. Default is false. + + Returns: + -------- + has_error : bool + True if at least one of the children is of type error_t, + False otherwise. + """ - return self.container + test_set = self.errors if direct_children_only else self.all_errors + return [isinstance(err, error_t) for err in test_set].any() def add_error(self, error): - """Add an error to this TransactionError. + """Add an error as a direct child to this TransactionError. @param error: An EntityError or a list of EntityErrors. @@ -216,7 +202,7 @@ class TransactionError(CaosDBException): self.add_error(e) return self - elif isinstance(error, TransactionError): + elif isinstance(error, EntityError): self.errors.append(error) return self