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