]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/services/exception.py
update source to Ceph Pacific 16.2.2
[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 import logging
6 from contextlib import contextmanager
7
8 import cherrypy
9 import rados
10 import rbd
11 from orchestrator import OrchestratorError
12
13 from ..exceptions import DashboardException, ViewCacheNoDataException
14 from ..services.ceph_service import SendCommandError
15
16 logger = logging.getLogger('exception')
17
18
19 def serialize_dashboard_exception(e, include_http_status=False, task=None):
20 """
21 :type e: Exception
22 :param include_http_status: Used for Tasks, where the HTTP status code is not available.
23 """
24 from ..tools import ViewCache
25 if isinstance(e, ViewCacheNoDataException):
26 return {'status': ViewCache.VALUE_NONE, 'value': None}
27
28 out = dict(detail=str(e))
29 try:
30 out['code'] = e.code
31 except AttributeError:
32 pass
33 component = getattr(e, 'component', None)
34 out['component'] = component if component else None
35 if include_http_status:
36 out['status'] = getattr(e, 'status', 500)
37 if task:
38 out['task'] = dict(name=task.name, metadata=task.metadata) # type: ignore
39 return out
40
41
42 # pylint: disable=broad-except
43 def dashboard_exception_handler(handler, *args, **kwargs):
44 try:
45 with handle_rados_error(component=None): # make the None controller the fallback.
46 return handler(*args, **kwargs)
47 # Don't catch cherrypy.* Exceptions.
48 except (ViewCacheNoDataException, DashboardException) as error:
49 logger.exception('Dashboard Exception')
50 cherrypy.response.headers['Content-Type'] = 'application/json'
51 cherrypy.response.status = getattr(error, 'status', 400)
52 return json.dumps(serialize_dashboard_exception(error)).encode('utf-8')
53 except Exception as error:
54 logger.exception('Internal Server Error')
55 raise error
56
57
58 @contextmanager
59 def handle_rbd_error():
60 try:
61 yield
62 except rbd.OSError as e:
63 raise DashboardException(e, component='rbd')
64 except rbd.Error as e:
65 raise DashboardException(e, component='rbd', code=e.__class__.__name__)
66
67
68 @contextmanager
69 def handle_rados_error(component):
70 try:
71 yield
72 except rados.OSError as e:
73 raise DashboardException(e, component=component)
74 except rados.Error as e:
75 raise DashboardException(e, component=component, code=e.__class__.__name__)
76
77
78 @contextmanager
79 def handle_send_command_error(component):
80 try:
81 yield
82 except SendCommandError as e:
83 raise DashboardException(e, component=component)
84
85
86 @contextmanager
87 def handle_orchestrator_error(component):
88 try:
89 yield
90 except OrchestratorError as e:
91 raise DashboardException(e, component=component)