Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / errors.py @ 6069b53b

History | View | Annotate | Download (3.8 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import logging
35

    
36
sendlog = logging.getLogger('clients.send')
37
recvlog = logging.getLogger('clients.recv')
38

    
39

    
40
class CLIError(Exception):
41
    def __init__(self, message, details=[], importance=0):
42
        """
43
        @message is the main message of the Error
44
        @detauls is a list of previous errors
45
        @importance of the output for the user
46
            Suggested values: 0, 1, 2, 3
47
        """
48
        message += '' if message and message[-1] == '\n' else '\n'
49
        super(CLIError, self).__init__(message)
50
        self.details = details if isinstance(details, list)\
51
            else [] if details is None else ['%s' % details]
52
        try:
53
            self.importance = int(importance)
54
        except ValueError:
55
            self.importance = 0
56

    
57

    
58
class CLISyntaxError(CLIError):
59
    def __init__(self, message='Syntax Error', details=[], importance=1):
60
        super(CLISyntaxError, self).__init__(message, details, importance)
61

    
62

    
63
class CLIUnknownCommand(CLIError):
64
    def __init__(self, message='Unknown Command', details=[], importance=1):
65
        super(CLIUnknownCommand, self).__init__(message, details, importance)
66

    
67

    
68
class CLICmdSpecError(CLIError):
69
    def __init__(self,
70
        message='Command Specification Error', details=[], importance=0):
71
        super(CLICmdSpecError, self).__init__(message, details, importance)
72

    
73

    
74
class CLICmdIncompleteError(CLICmdSpecError):
75
    def __init__(self,
76
        message='Incomplete Command Error', details=[], importance=1):
77
        super(CLICmdSpecError, self).__init__(message, details, importance)
78

    
79

    
80
def raiseCLIError(err, message='', importance=0, details=[]):
81
    from traceback import format_stack
82
    recvlog.debug('\n'.join(['%s' % type(err)] + format_stack()))
83

    
84
    origerr = '%s' % err
85
    origerr = origerr if origerr else '%s' % type(err)
86

    
87
    if message:
88
        details.append(origerr)
89
    else:
90
        message = origerr
91

    
92
    try:
93
        status = err.status
94
    except AttributeError:
95
        status = None
96

    
97
    try:
98
        details.append(err.details)
99
    except AttributeError:
100
        pass
101

    
102
    if status:
103
        message = '(%s) %s' % (err.status, message)
104
        try:
105
            status = int(err.status)
106
        except ValueError:
107
            raise CLIError(message, details, importance)
108
        importance = status // 100
109
    raise CLIError(message, details, importance)