root / kamaki / cli / errors.py @ bb50c4ec
History | View | Annotate | Download (6 kB)
1 |
# Copyright 2013 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 |
from kamaki.cli.logger import get_logger |
35 |
|
36 |
log = get_logger('kamaki.cli')
|
37 |
|
38 |
|
39 |
class CLIError(Exception): |
40 |
|
41 |
def __init__(self, message, details=[], importance=0): |
42 |
"""
|
43 |
:param message: is the main message of the Error
|
44 |
:param defaults: is a list of previous errors
|
45 |
:param importance: of the output for the user (0, 1, 2, 3)
|
46 |
"""
|
47 |
message += '' if message and message.endswith('\n') else '\n' |
48 |
super(CLIError, self).__init__(message) |
49 |
self.details = (list(details) if ( |
50 |
isinstance(details, list) or isinstance(details, tuple)) else [ |
51 |
'%s' % details]) if details else [] |
52 |
try:
|
53 |
self.importance = int(importance or 0) |
54 |
except ValueError: |
55 |
self.importance = 0 |
56 |
|
57 |
|
58 |
class CLIUnimplemented(CLIError): |
59 |
def __init__( |
60 |
self,
|
61 |
message='I \'M SORRY, DAVE.\nI \'M AFRAID I CAN\'T DO THAT.',
|
62 |
details=[ |
63 |
' _ |',
|
64 |
' _-- --_ |',
|
65 |
' -- -- |',
|
66 |
' -- . -- |',
|
67 |
' -_ _- |',
|
68 |
' -_ _- |',
|
69 |
' - |'],
|
70 |
importance=3):
|
71 |
super(CLIUnimplemented, self).__init__(message, details, importance) |
72 |
|
73 |
|
74 |
class CLIBaseUrlError(CLIError): |
75 |
def __init__(self, message='', details=[], importance=2, service=None): |
76 |
service = '%s' % (service or '') |
77 |
message = message or 'No URL for %s' % service.lower() |
78 |
details = details or [
|
79 |
'Two ways to resolve this:',
|
80 |
'(Use the correct cloud name, instead of "default")',
|
81 |
'A. (recommended) Let kamaki discover endpoint URLs for all',
|
82 |
'services by setting a single Authentication URL and token:',
|
83 |
' /config set cloud.default.url <AUTH_URL>',
|
84 |
' /config set cloud.default.token <t0k3n>']
|
85 |
super(CLIBaseUrlError, self).__init__(message, details, importance) |
86 |
|
87 |
|
88 |
class CLISyntaxError(CLIError): |
89 |
def __init__(self, message='Syntax Error', details=[], importance=1): |
90 |
super(CLISyntaxError, self).__init__(message, details, importance) |
91 |
|
92 |
|
93 |
class CLIInvalidArgument(CLISyntaxError): |
94 |
def __init__(self, message='Invalid Argument', details=[], importance=1): |
95 |
super(CLIInvalidArgument, self).__init__(message, details, importance) |
96 |
|
97 |
|
98 |
class CLIUnknownCommand(CLIError): |
99 |
def __init__(self, message='Unknown Command', details=[], importance=1): |
100 |
super(CLIUnknownCommand, self).__init__(message, details, importance) |
101 |
|
102 |
|
103 |
class CLICmdSpecError(CLIError): |
104 |
def __init__( |
105 |
self, message='Command Specification Error', |
106 |
details=[], importance=0):
|
107 |
super(CLICmdSpecError, self).__init__(message, details, importance) |
108 |
|
109 |
|
110 |
def raiseCLIError(err, message='', importance=0, details=[]): |
111 |
"""
|
112 |
:param err: (Exception) the original error message, if None, a new
|
113 |
CLIError is born which is conceptually bind to raiser
|
114 |
|
115 |
:param message: (str) a custom error message that overrides err's
|
116 |
|
117 |
:param importance: (int) instruction to called application (e.g. for
|
118 |
coloring printed error messages)
|
119 |
|
120 |
:param details: (list) various information on the error
|
121 |
|
122 |
:raises CLIError: it is the purpose of this method
|
123 |
"""
|
124 |
from traceback import format_stack |
125 |
|
126 |
stack = ['%s' % type(err)] if err else ['<kamaki.cli.errors.CLIError>'] |
127 |
stack += format_stack() |
128 |
|
129 |
details = list(details) if ( |
130 |
isinstance(details, list) or isinstance(details, tuple)) else [ |
131 |
'%s' % details]
|
132 |
err_details = getattr(err, 'details', []) |
133 |
if isinstance(err_details, list) or isinstance(err_details, tuple): |
134 |
details += list(err_details)
|
135 |
else:
|
136 |
details.append('%s' % err_details)
|
137 |
|
138 |
origerr = (('%s' % err) or '%s' % type(err)) if err else stack[0] |
139 |
message = '%s' % message or origerr |
140 |
|
141 |
try:
|
142 |
status = err.status or err.errno
|
143 |
except AttributeError: |
144 |
try:
|
145 |
status = err.errno |
146 |
except AttributeError: |
147 |
status = None
|
148 |
|
149 |
if origerr not in details + [message]: |
150 |
details.append(origerr) |
151 |
|
152 |
message += '' if message and message.endswith('\n') else '\n' |
153 |
if status:
|
154 |
message = '(%s) %s' % (status, message)
|
155 |
try:
|
156 |
status = int(status)
|
157 |
except ValueError: |
158 |
raise CLIError(message, details, importance or 0) |
159 |
importance = importance or status // 100 |
160 |
importance = getattr(err, 'importance', importance) |
161 |
raise CLIError(message, details, importance)
|