Revision 0238c167 kamaki/cli/errors.py

b/kamaki/cli/errors.py
33 33

  
34 34

  
35 35
class CLIError(Exception):
36
    def __init__(self, message, status=0, details='', importance=0):
37
        """importance is set by the raiser
38
        0 is the lowest possible importance
39
        Suggested values: 0, 1, 2, 3
36
    def __init__(self, message, details=[], importance=0):
40 37
        """
41
        super(CLIError, self).__init__(message, status, details)
42
        self.message = message
43
        self.status = status
44
        self.details = details
45
        self.importance = importance
46

  
47
    def __unicode__(self):
48
        return unicode(self.message)
38
        @message is the main message of the Error
39
        @detauls is a list of previous errors
40
        @importance of the output for the user
41
            Suggested values: 0, 1, 2, 3
42
        """
43
        super(CLIError, self).__init__(message)
44
        self.details = details if isinstance(details, list)\
45
            else [] if details is None else ['%s' % details]
46
        try:
47
            self.importance = int(importance)
48
        except ValueError:
49
            self.importance = 0
49 50

  
50 51

  
51 52
class CLISyntaxError(CLIError):
52
    def __init__(self, message='Syntax Error', status=10, details=''):
53
        super(CLISyntaxError,
54
            self).__init__(message, status, details, importance=1)
53
    def __init__(self, message='Syntax Error', details=[], importance=1):
54
        super(CLISyntaxError, self).__init__(message, details, importance)
55 55

  
56 56

  
57 57
class CLIUnknownCommand(CLIError):
58
    def __init__(self, message='Unknown Command', status=12, details=''):
59
        super(CLIUnknownCommand,
60
            self).__init__(message, status, details, importance=1)
58
    def __init__(self, message='Unknown Command', details=[], importance=1):
59
        super(CLIUnknownCommand, self).__init__(message, details, importance)
61 60

  
62 61

  
63 62
class CLICmdSpecError(CLIError):
64 63
    def __init__(self,
65
        message='Command Specification Error',
66
        status=13,
67
        details='',
68
        importance=1):
69
        super(CLICmdSpecError,
70
            self).__init__(message, status, details, importance=0)
64
        message='Command Specification Error', details=[], importance=0):
65
        super(CLICmdSpecError, self).__init__(message, details, importance)
71 66

  
72 67

  
73 68
class CLICmdIncompleteError(CLICmdSpecError):
74 69
    def __init__(self,
75
        message='Incomplete Command Error',
76
        status=14,
77
        details=''):
78
        super(CLICmdSpecError,
79
            self).__init__(message, status, details, importance=1)
70
        message='Incomplete Command Error', details=[], importance=1):
71
        super(CLICmdSpecError, self).__init__(message, details, importance)
80 72

  
81 73

  
82
def raiseCLIError(err, importance=-1):
83
    if importance < 0:
84
        if err.status <= 0:
85
            importance = 0
86
        elif err.status <= 400:
87
            importance = 1
88
        elif err.status <= 500:
89
            importance = 2
90
        else:
91
            importance = 3
92
    raise CLIError('%s' % err, err.status, err.details, importance)
74
def raiseCLIError(err, importance=0):
75
    message = '%s' % err
76
    if err.status:
77
        message = '(%s) %s' % (err.status, message)
78
        try:
79
            status = int(err.status)
80
        except ValueError:
81
            raise CLIError(message, err.details, importance)
82
        importance = status // 100
83
    raise CLIError(message, err.details, importance)

Also available in: Unified diff