root / kamaki / cli / errors.py @ c17b8bc0
History | View | Annotate | Download (4.7 kB)
1 | c9e706b2 | Stavros Sachtouris | # Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | c9e706b2 | Stavros Sachtouris | #
|
3 | c9e706b2 | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | c9e706b2 | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | c9e706b2 | Stavros Sachtouris | # conditions are met:
|
6 | c9e706b2 | Stavros Sachtouris | #
|
7 | c9e706b2 | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | c9e706b2 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | c9e706b2 | Stavros Sachtouris | # disclaimer.
|
10 | c9e706b2 | Stavros Sachtouris | #
|
11 | c9e706b2 | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | c9e706b2 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | c9e706b2 | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | c9e706b2 | Stavros Sachtouris | # provided with the distribution.
|
15 | c9e706b2 | Stavros Sachtouris | #
|
16 | c9e706b2 | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | c9e706b2 | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | c9e706b2 | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | c9e706b2 | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | c9e706b2 | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | c9e706b2 | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | c9e706b2 | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | c9e706b2 | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | c9e706b2 | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | c9e706b2 | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | c9e706b2 | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | c9e706b2 | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | c9e706b2 | Stavros Sachtouris | #
|
29 | c9e706b2 | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | c9e706b2 | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | c9e706b2 | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | c9e706b2 | Stavros Sachtouris | # or implied, of GRNET S.A.
|
33 | c9e706b2 | Stavros Sachtouris | |
34 | 6069b53b | Stavros Sachtouris | import logging |
35 | 6069b53b | Stavros Sachtouris | |
36 | 6069b53b | Stavros Sachtouris | sendlog = logging.getLogger('clients.send')
|
37 | 6069b53b | Stavros Sachtouris | recvlog = logging.getLogger('clients.recv')
|
38 | de4f08ef | Stavros Sachtouris | |
39 | fd5db045 | Stavros Sachtouris | |
40 | c9e706b2 | Stavros Sachtouris | class CLIError(Exception): |
41 | 0238c167 | Stavros Sachtouris | def __init__(self, message, details=[], importance=0): |
42 | c9e706b2 | Stavros Sachtouris | """
|
43 | 0238c167 | Stavros Sachtouris | @message is the main message of the Error
|
44 | 0238c167 | Stavros Sachtouris | @detauls is a list of previous errors
|
45 | 0238c167 | Stavros Sachtouris | @importance of the output for the user
|
46 | 0238c167 | Stavros Sachtouris | Suggested values: 0, 1, 2, 3
|
47 | 0238c167 | Stavros Sachtouris | """
|
48 | 6069b53b | Stavros Sachtouris | message += '' if message and message[-1] == '\n' else '\n' |
49 | 0238c167 | Stavros Sachtouris | super(CLIError, self).__init__(message) |
50 | 0238c167 | Stavros Sachtouris | self.details = details if isinstance(details, list)\ |
51 | 0238c167 | Stavros Sachtouris | else [] if details is None else ['%s' % details] |
52 | 0238c167 | Stavros Sachtouris | try:
|
53 | 0238c167 | Stavros Sachtouris | self.importance = int(importance) |
54 | 0238c167 | Stavros Sachtouris | except ValueError: |
55 | 0238c167 | Stavros Sachtouris | self.importance = 0 |
56 | c9e706b2 | Stavros Sachtouris | |
57 | fd5db045 | Stavros Sachtouris | |
58 | c9e706b2 | Stavros Sachtouris | class CLISyntaxError(CLIError): |
59 | 0238c167 | Stavros Sachtouris | def __init__(self, message='Syntax Error', details=[], importance=1): |
60 | 0238c167 | Stavros Sachtouris | super(CLISyntaxError, self).__init__(message, details, importance) |
61 | fd5db045 | Stavros Sachtouris | |
62 | c9e706b2 | Stavros Sachtouris | |
63 | c9e706b2 | Stavros Sachtouris | class CLIUnknownCommand(CLIError): |
64 | 0238c167 | Stavros Sachtouris | def __init__(self, message='Unknown Command', details=[], importance=1): |
65 | 0238c167 | Stavros Sachtouris | super(CLIUnknownCommand, self).__init__(message, details, importance) |
66 | fd5db045 | Stavros Sachtouris | |
67 | c9e706b2 | Stavros Sachtouris | |
68 | 926d6863 | Stavros Sachtouris | class CLICmdSpecError(CLIError): |
69 | fd5db045 | Stavros Sachtouris | def __init__(self, |
70 | 0238c167 | Stavros Sachtouris | message='Command Specification Error', details=[], importance=0): |
71 | 0238c167 | Stavros Sachtouris | super(CLICmdSpecError, self).__init__(message, details, importance) |
72 | fd5db045 | Stavros Sachtouris | |
73 | c9e706b2 | Stavros Sachtouris | |
74 | 017d37ce | Stavros Sachtouris | class CLICmdIncompleteError(CLICmdSpecError): |
75 | fd5db045 | Stavros Sachtouris | def __init__(self, |
76 | 0238c167 | Stavros Sachtouris | message='Incomplete Command Error', details=[], importance=1): |
77 | 0238c167 | Stavros Sachtouris | super(CLICmdSpecError, self).__init__(message, details, importance) |
78 | fd5db045 | Stavros Sachtouris | |
79 | 017d37ce | Stavros Sachtouris | |
80 | 6069b53b | Stavros Sachtouris | def raiseCLIError(err, message='', importance=0, details=[]): |
81 | 83ba5545 | Stavros Sachtouris | """
|
82 | 83ba5545 | Stavros Sachtouris | :param err: (Exception) the original error message, if None, a new
|
83 | 83ba5545 | Stavros Sachtouris | CLIError is born which is conceptually bind to raiser
|
84 | 83ba5545 | Stavros Sachtouris |
|
85 | 83ba5545 | Stavros Sachtouris | :param message: (str) a custom error message that overrides err's
|
86 | 83ba5545 | Stavros Sachtouris |
|
87 | 83ba5545 | Stavros Sachtouris | :param importance: (int) instruction to called application (e.g. for
|
88 | 83ba5545 | Stavros Sachtouris | coloring printed error messages)
|
89 | 83ba5545 | Stavros Sachtouris |
|
90 | 83ba5545 | Stavros Sachtouris | :param details: (list) various information on the error
|
91 | 83ba5545 | Stavros Sachtouris |
|
92 | 83ba5545 | Stavros Sachtouris | :raises CLIError: it is the purpose of this method
|
93 | 83ba5545 | Stavros Sachtouris | """
|
94 | 6069b53b | Stavros Sachtouris | from traceback import format_stack |
95 | 83ba5545 | Stavros Sachtouris | stack = ['%s' % type(err)] if err else ['<kamaki.cli.errors.CLIError>'] |
96 | 3667e969 | Stavros Sachtouris | stack += format_stack() |
97 | 3667e969 | Stavros Sachtouris | try:
|
98 | 3667e969 | Stavros Sachtouris | stack = [e for e in stack if e != stack[1]] |
99 | 3667e969 | Stavros Sachtouris | except KeyError: |
100 | 3667e969 | Stavros Sachtouris | recvlog.debug('\n < '.join(stack))
|
101 | 83ba5545 | Stavros Sachtouris | |
102 | 3e0f2e53 | Stavros Sachtouris | details = ['%s' % details] if not isinstance(details, list)\ |
103 | 3e0f2e53 | Stavros Sachtouris | else list(details) |
104 | 6069b53b | Stavros Sachtouris | |
105 | 83ba5545 | Stavros Sachtouris | if err:
|
106 | 83ba5545 | Stavros Sachtouris | origerr = '%s' % err
|
107 | 83ba5545 | Stavros Sachtouris | origerr = origerr if origerr else '%s' % type(err) |
108 | 83ba5545 | Stavros Sachtouris | else:
|
109 | 3667e969 | Stavros Sachtouris | origerr = stack[0]
|
110 | 6069b53b | Stavros Sachtouris | |
111 | 3e0f2e53 | Stavros Sachtouris | message = unicode(message) if message else unicode(origerr) |
112 | 6069b53b | Stavros Sachtouris | |
113 | 6069b53b | Stavros Sachtouris | try:
|
114 | 6069b53b | Stavros Sachtouris | status = err.status |
115 | 6069b53b | Stavros Sachtouris | except AttributeError: |
116 | 6069b53b | Stavros Sachtouris | status = None
|
117 | 6069b53b | Stavros Sachtouris | |
118 | 3667e969 | Stavros Sachtouris | if origerr not in details + [message]: |
119 | 3667e969 | Stavros Sachtouris | details.append(origerr) |
120 | 6069b53b | Stavros Sachtouris | try:
|
121 | 6069b53b | Stavros Sachtouris | details.append(err.details) |
122 | 6069b53b | Stavros Sachtouris | except AttributeError: |
123 | 6069b53b | Stavros Sachtouris | pass
|
124 | 6069b53b | Stavros Sachtouris | |
125 | 3667e969 | Stavros Sachtouris | message += '' if message and message[-1] == '\n' else '\n' |
126 | 6069b53b | Stavros Sachtouris | if status:
|
127 | 6069b53b | Stavros Sachtouris | message = '(%s) %s' % (err.status, message)
|
128 | 0238c167 | Stavros Sachtouris | try:
|
129 | 0238c167 | Stavros Sachtouris | status = int(err.status)
|
130 | 0238c167 | Stavros Sachtouris | except ValueError: |
131 | 6069b53b | Stavros Sachtouris | raise CLIError(message, details, importance)
|
132 | 0238c167 | Stavros Sachtouris | importance = status // 100
|
133 | 6069b53b | Stavros Sachtouris | raise CLIError(message, details, importance) |