Skip to content
Snippets Groups Projects
Forked from DM / dm-docs
261 commits behind, 599 commits ahead of the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
dmExceptionMapper.py 1.12 KiB
#!/usr/bin/env python

from dm.common.constants import dmStatus
from dm.common.exceptions import dmExceptionMap
from dm.common.exceptions.dmException import DmException

class DmExceptionMapper:

    @classmethod
    def checkStatus(cls, httpHeaders):
        """ Map dm status code into appropriate exception. """
        code = httpHeaders.get('Dm-Status-Code', None)
        msg = httpHeaders.get('Dm-Status-Message', 'Internal Error')
        if code is None or code == str(dmStatus.DM_OK):
            return
        elif dmExceptionMap.DM_EXCEPTION_MAP.has_key(int(code)):
            # Exception string is value of the form 'x.y.z'
            # where 'x.y' is dm module, and 'z' class in that module
            exStr = dmExceptionMap.DM_EXCEPTION_MAP.get(int(code))
            exClass = exStr.split('.')[-1] # 'z' in 'x.y.z'
            exModule = '.'.join(exStr.split('.')[:-1]) # 'x.y' in 'x.y.z'
            exec 'from dm.common.exceptions.%s import %s' % (exModule, exClass)
            exec 'ex = %s(msg)' % (exClass)
            raise ex
        else:
            raise DmException(msg)

# Testing.
if __name__ == '__main__':
    pass