1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import json
import sys
from contextlib import contextmanager
import cherrypy
import rbd
import rados
from .. import logger
from ..services.ceph_service import SendCommandError
from ..exceptions import ViewCacheNoDataException, DashboardException
from ..tools import wraps
if sys.version_info < (3, 0):
# Monkey-patch a __call__ method into @contextmanager to make
# it compatible to Python 3
from contextlib import GeneratorContextManager # pylint: disable=no-name-in-module
def init(self, *args):
if len(args) == 1:
self.gen = args[0]
elif len(args) == 3:
self.func, self.args, self.kwargs = args
else:
raise TypeError()
def enter(self):
if hasattr(self, 'func'):
self.gen = self.func(*self.args, **self.kwargs)
try:
return self.gen.next()
except StopIteration:
raise RuntimeError("generator didn't yield")
def call(self, f):
@wraps(f)
def wrapper(*args, **kwargs):
with self:
return f(*args, **kwargs)
return wrapper
GeneratorContextManager.__init__ = init
GeneratorContextManager.__enter__ = enter
GeneratorContextManager.__call__ = call
# pylint: disable=function-redefined
def contextmanager(func):
@wraps(func)
def helper(*args, **kwds):
return GeneratorContextManager(func, args, kwds)
return helper
def serialize_dashboard_exception(e, include_http_status=False, task=None):
"""
:type e: Exception
:param include_http_status: Used for Tasks, where the HTTP status code is not available.
"""
from ..tools import ViewCache
if isinstance(e, ViewCacheNoDataException):
return {'status': ViewCache.VALUE_NONE, 'value': None}
out = dict(detail=str(e))
try:
out['code'] = e.code
except AttributeError:
pass
component = getattr(e, 'component', None)
out['component'] = component if component else None
if include_http_status:
out['status'] = getattr(e, 'status', 500)
if task:
out['task'] = dict(name=task.name, metadata=task.metadata)
return out
def dashboard_exception_handler(handler, *args, **kwargs):
try:
with handle_rados_error(component=None): # make the None controller the fallback.
return handler(*args, **kwargs)
# Don't catch cherrypy.* Exceptions.
except (ViewCacheNoDataException, DashboardException) as e:
logger.exception('dashboard_exception_handler')
cherrypy.response.headers['Content-Type'] = 'application/json'
cherrypy.response.status = getattr(e, 'status', 400)
return json.dumps(serialize_dashboard_exception(e)).encode('utf-8')
@contextmanager
def handle_rbd_error():
try:
yield
except rbd.OSError as e:
raise DashboardException(e, component='rbd')
except rbd.Error as e:
raise DashboardException(e, component='rbd', code=e.__class__.__name__)
@contextmanager
def handle_rados_error(component):
try:
yield
except rados.OSError as e:
raise DashboardException(e, component=component)
except rados.Error as e:
raise DashboardException(e, component=component, code=e.__class__.__name__)
@contextmanager
def handle_send_command_error(component):
try:
yield
except SendCommandError as e:
raise DashboardException(e, component=component)
|