Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message handling #99

Closed
psykokwak4 opened this issue May 3, 2016 · 1 comment
Closed

Error message handling #99

psykokwak4 opened this issue May 3, 2016 · 1 comment

Comments

@psykokwak4
Copy link
Contributor

psykokwak4 commented May 3, 2016

Currently we have two separate exception classes in errors module,

class YPYErrorCode(Enum):
    INVALID_UNION_VALUE = 'Cannot translate union value'
    INVALID_ENCODE_VALUE = 'Cannot encode value'
    INVALID_HIERARCHY_PARENT = 'Parent is not set. \
                    Parent Hierarchy cannot be determined'
    INVALID_HIERARCHY_KEY = 'Key value is not set. \
                    Parent hierarchy cannot be constructed'
    INVALID_RPC = 'Object is not an RPC, cannot execute non-RPC object.'
    INVALID_MODIFY = 'Entity is read-only, cannot modify a read-only entity.'
    SERVER_REJ = 'Server rejected request.'
    SERVER_COMMIT_ERR = 'Server reported an error while committing change.'


class YPYError(Exception):
    ''' Base Exception for YDK Errors '''
    def __init__(self, error_code=None, error_msg=None):
        self.error_code = error_code
        self.error_msg = error_msg

    def __str__(self):
        ret = []
        if self.error_code is not None:
            ret.append(self.error_code.value)
        if self.error_msg is not None:
            parser = etree.XMLParser(remove_blank_text=True)
            root = etree.XML(self.error_msg.xml, parser)
            for r in root.iter():
                tag = r.tag[r.tag.rfind('}') + 1 :]
                if r.text is not None:
                    ret.append('\t{}: {}'.format(tag, r.text.strip()))
        return '\n'.join(ret)


class YPYDataValidationError(YPYError):
    '''
    Exception for Client Side Data Validation

    Type Validation\n
    --------
    Any data validation error encountered that is related to type \
    validation encountered does not raise an Exception right away. 

    To uncover as many client side issues as possible, \
    an i_errors list is injected in the parent entity of any entity \
    with issues. The items added to this i_errors list captures the \
    object type that caused the error as well as an error message.

    '''
    pass

The YPYError class deals with server side error, and will populate error messages sent back by the device. The second one is for validation error, it use i_errors to collect as many client side issues as possible. Would it be a good practice to use a single error class, and populate error messages collected by i_errors to the enum YPYErrorCode, to reduce redundancy?

@ghost
Copy link

ghost commented Jun 23, 2016

Fixed by #133

@ghost ghost closed this as completed Jun 23, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants