root / kamaki / clients / commissioning / exception.py @ 54069d1b
History | View | Annotate | Download (3.7 kB)
1 |
# Copyright 2012 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 |
|
35 |
class CallError(Exception): |
36 |
exceptions = {} |
37 |
|
38 |
def __new__(cls, *args, **kw): |
39 |
call_error = kw.get('call_error', None) |
40 |
if call_error is None: |
41 |
call_error = cls.__name__ |
42 |
else:
|
43 |
call_error = str(call_error)
|
44 |
cls = CallError.exceptions.get(call_error, cls) |
45 |
self = Exception.__new__(cls) |
46 |
return self |
47 |
|
48 |
def __init__(self, *args, **kw): |
49 |
self.call_error = kw.get('call_error', self.__class__.__name__) |
50 |
self.args = args
|
51 |
|
52 |
def __str__(self): |
53 |
return '\n--------\n'.join(str(x) for x in self.args) |
54 |
|
55 |
def __repr__(self): |
56 |
return '%s(%s)' % (self.__class__.__name__, |
57 |
','.join(str(x) for x in self.args)) |
58 |
|
59 |
@classmethod
|
60 |
def from_exception(cls, exc): |
61 |
args = None
|
62 |
try:
|
63 |
args = tuple(exc.args)
|
64 |
except (TypeError, AttributeError), e: |
65 |
pass
|
66 |
|
67 |
if args is None: |
68 |
args = (str(exc),)
|
69 |
self = cls(*args, call_error=exc.__class__.__name__)
|
70 |
return self |
71 |
|
72 |
def to_dict(self): |
73 |
return {'call_error': self.call_error, |
74 |
'error_args': self.args} |
75 |
|
76 |
@classmethod
|
77 |
def from_dict(cls, dictobj): |
78 |
args = None
|
79 |
try:
|
80 |
if 'error_args' in dictobj and 'call_error' in dictobj: |
81 |
args = dictobj['error_args']
|
82 |
call_error = dictobj['call_error']
|
83 |
except TypeError, e: |
84 |
pass
|
85 |
|
86 |
if args is None: |
87 |
args = (str(dictobj),)
|
88 |
call_error = 'UnknownError'
|
89 |
|
90 |
self = cls(*args, call_error=call_error)
|
91 |
return self |
92 |
|
93 |
def register_exceptions(*exceptions): |
94 |
for exception in exceptions: |
95 |
if not issubclass(exception, CallError): |
96 |
m = "Registering '%s': is not a CallError subclass" % (exception,)
|
97 |
raise ValueError(m) |
98 |
CallError.exceptions[exception.__name__] = exception |
99 |
|
100 |
def register_exception(exc): |
101 |
register_exceptions(exc) |
102 |
return exc
|
103 |
|
104 |
@register_exception
|
105 |
class CorruptedError(CallError): |
106 |
pass
|
107 |
|
108 |
@register_exception
|
109 |
class InvalidDataError(CallError): |
110 |
pass
|
111 |
|
112 |
class ReturnButFail(Exception): |
113 |
def __init__(self, retval=None): |
114 |
self.data = retval
|