]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/services/exception.py
import ceph quincy 17.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / services / exception.py
1 # -*- coding: utf-8 -*-
2
3 import json
4 import logging
5 from contextlib import contextmanager
6
7 import cephfs
8 import cherrypy
9 import rados
10 import rbd
11 from orchestrator import OrchestratorError
12
13 from ..exceptions import DashboardException, ViewCacheNoDataException
14 from ..rest_client import RequestException
15 from ..services.ceph_service import SendCommandError
16
17 logger = logging.getLogger('exception')
18
19
20 def serialize_dashboard_exception(e, include_http_status=False, task=None):
21 """
22 :type e: Exception
23 :param include_http_status: Used for Tasks, where the HTTP status code is not available.
24 """
25 from ..tools import ViewCache
26 if isinstance(e, ViewCacheNoDataException):
27 return {'status': ViewCache.VALUE_NONE, 'value': None}
28
29 out = dict(detail=str(e))
30 try:
31 out['code'] = e.code
32 except AttributeError:
33 pass
34 component = getattr(e, 'component', None)
35 out['component'] = component if component else None
36 if include_http_status:
37 out['status'] = getattr(e, 'status', 500)
38 if task:
39 out['task'] = dict(name=task.name, metadata=task.metadata) # type: ignore
40 return out
41
42
43 # pylint: disable=broad-except
44 def dashboard_exception_handler(handler, *args, **kwargs):
45 try:
46 with handle_rados_error(component=None): # make the None controller the fallback.
47 return handler(*args, **kwargs)
48 # Don't catch cherrypy.* Exceptions.
49 except (ViewCacheNoDataException, DashboardException) as error:
50 logger.exception('Dashboard Exception')
51 cherrypy.response.headers['Content-Type'] = 'application/json'
52 cherrypy.response.status = getattr(error, 'status', 400)
53 return json.dumps(serialize_dashboard_exception(error)).encode('utf-8')
54 except cherrypy.HTTPRedirect:
55 # No internal errors
56 raise
57 except Exception as error:
58 logger.exception('Internal Server Error')
59 raise error
60
61
62 @contextmanager
63 def handle_cephfs_error():
64 try:
65 yield
66 except cephfs.OSError as e:
67 raise DashboardException(e, component='cephfs') from e
68
69
70 @contextmanager
71 def handle_rbd_error():
72 try:
73 yield
74 except rbd.OSError as e:
75 raise DashboardException(e, component='rbd')
76 except rbd.Error as e:
77 raise DashboardException(e, component='rbd', code=e.__class__.__name__)
78
79
80 @contextmanager
81 def handle_rados_error(component):
82 try:
83 yield
84 except rados.OSError as e:
85 raise DashboardException(e, component=component)
86 except rados.Error as e:
87 raise DashboardException(e, component=component, code=e.__class__.__name__)
88
89
90 @contextmanager
91 def handle_send_command_error(component):
92 try:
93 yield
94 except SendCommandError as e:
95 raise DashboardException(e, component=component)
96
97
98 @contextmanager
99 def handle_orchestrator_error(component):
100 try:
101 yield
102 except OrchestratorError as e:
103 raise DashboardException(e, component=component)
104
105
106 @contextmanager
107 def handle_request_error(component):
108 try:
109 yield
110 except RequestException as e:
111 if e.content:
112 content = json.loads(e.content)
113 content_message = content.get('message')
114 if content_message:
115 raise DashboardException(
116 msg=content_message, component=component)
117 raise DashboardException(e=e, component=component)
118
119
120 @contextmanager
121 def handle_error(component, http_status_code=None):
122 try:
123 yield
124 except Exception as e: # pylint: disable=broad-except
125 raise DashboardException(e, component=component, http_status_code=http_status_code)