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