def error(self):
        """
        Get the error-related properties.

        :return: The error-related properties combined
            into an instance of :class:`systemlink.messagebus.error.Error`.
        :rtype: systemlink.messagebus.error.Error or None
        """
        error_code_str = self.get_property(ERROR_CODE_PROPERTY)
        error_info_str = self.get_property(ERROR_INFO_PROPERTY)
        idx = 0
        args = []
        while True:
            arg_prop_name = '{0}{1}'.format(ERROR_ARGS_PROPERTY, idx)
            arg = self.get_property(arg_prop_name)
            if not arg:
                break
            args.append(arg)
            idx += 1

        if error_code_str:
            error_code_obj = error_code_registry.lookup(error_code_str)
        else:
            error_code_obj = error_code_registry.lookup('Unknown')

        if not error_info_str:
            error_info_str = ''

        error_obj = Error(error_code_obj, error_info_str, args)
        return error_obj
Beispiel #2
0
    def from_exception(cls, exc, include_stack=True):
        """
        Create an :class:`Error` instance based on an exception.

        :param exc: An instance of a class derived from
            :class:`Exception`.
        :type exc: Exception
        :param include_stack: Whether to include stack information in
            the error message.
        :type include_stack: bool
        :return: An instance of :class:`Error`.
        :rtype: Error
        """
        # Import here instead of at the top-level to avoid a circular
        # dependency.
        from systemlink.messagebus.exceptions import SystemLinkException

        if isinstance(exc, SystemLinkException):
            return exc.error

        error_code_obj = error_code_registry.lookup('Skyline.Exception')

        if include_stack:
            exc_tb = sys.exc_info()[2]
            if exc_tb is not None:
                tb_info = traceback.format_tb(exc_tb)
            else:
                tb_info = []
            info = 'Python exception: {0}: {1}\n{2}'.format(
                type(exc).__name__, exc, ''.join(tb_info)).strip()
        else:
            info = 'Python exception: {0}: {1}'.format(
                type(exc).__name__, exc).strip()

        return cls(error_code=error_code_obj, info=info)
    def error_routed(self, qualified_name, error_args, routed_message,
                     response_type, *response_args, **response_kwargs):
        """
        Respond to a routed message with an error.

        :param qualified_name: The qualified name of the error. It is in
            the form ``[category].[name]``.
        :type qualified_name: str
        :param error_args: Arguments (if any) for the error code used.
        :type error_args: list or None
        :param routed_message: An incoming message to handle.
        :type routed_message: systemlink.messagebus.routed_message.RoutedMessage
        :param response_type: The type of the response message.
        :type response_type: object
        :param response_args: The arguments to initialize the response message
            based on the ``response_type``. Should not include the
            ``request_message``.
        :param response_args: tuple of arguments
        :type response_args: tuple
        :param response_kwargs: dict of arguments
        :type response_kwargs: dict
        """
        if '.' not in qualified_name:
            qualified_name = ERROR_CATEGORY + '.' + qualified_name
        error_code_obj = error_code_registry.lookup(qualified_name)
        error_obj = error.Error(error_code=error_code_obj, args=error_args)
        response_message = response_type(routed_message, *response_args,
                                         **response_kwargs)
        response_message.error = error_obj
        self.publish_routed_message(response_message)
Beispiel #4
0
 def __init__(self, error):
     """
     :param error: An instance of :class`systemlink.messagebus.error.Error`.
         May be ``None``.
     :type error: systemlink.messagebus.error.Error or None
     """
     if error is None:
         error_code_obj = error_code_registry.lookup('Skyline.Exception')
         error = Error(error_code=error_code_obj)
     message = error.info
     super(SystemLinkException, self).__init__(message)
     self.message = message
     self.error = error
Beispiel #5
0
    def from_name(cls, qualified_name, info=None, args=None):
        """
        Create an :class:`Error` instance based on the qualified name.

        :param qualified_name: The qualified name of the error. It is in
            the form ``[category].[name]``. May be ``None``.
        :type qualified_name: str or None
        :param info: Error information string. May be ``None`` in the cases
            of no error or if this information is desired to be auto-generated
            based on ``qualified_name`` and ``args``.
        :type info: str or None
        :param args: A list of arguments associated with the error. May be
            ``None`` if the associated error does not have any arguments or
            if ``info`` is not ``None``.
        :type args: list or None
        :return: An instance of :class:`Error`.
        :rtype: Error
        """
        if qualified_name is None:
            qualified_name = 'Skyline.Exception'
        error_code_obj = error_code_registry.lookup(qualified_name)
        return cls(error_code=error_code_obj, info=info, args=args)
Beispiel #6
0
    def from_error_code(cls, error_code, info=None, args=None):
        """
        Create a SystemLinkException object based on an ErrorCode object.

        :param error_code: An instance of
            :class`systemlink.messagebus.error_code.ErrorCode`. May be
            ``None``.
        :type error_code: systemlink.messagebus.error_code.ErrorCode or None
        :param info: Error information string. May be ``None`` in the cases
            of no error or if this information is desired to be auto-generated
            based on ``error_code`` and ``args``.
        :type info: str or None
        :param args: A list of arguments associated with the error. May be
            ``None`` if the associated error does not have any arguments or
            if ``info`` is not ``None``.
        :type args: list or None
        :return: An instance of :class:`SystemLinkException`.
        :rtype: SystemLinkException
        """
        if error_code is None:
            error_code = error_code_registry.lookup('Skyline.Exception')
        error_obj = Error(error_code=error_code, info=info, args=args)
        return cls(error=error_obj)