]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/services/exception.py
1 # -*- coding: utf-8 -*-
5 from contextlib
import contextmanager
11 from orchestrator
import OrchestratorError
13 from ..exceptions
import DashboardException
, ViewCacheNoDataException
14 from ..rest_client
import RequestException
15 from ..services
.ceph_service
import SendCommandError
17 logger
= logging
.getLogger('exception')
20 def serialize_dashboard_exception(e
, include_http_status
=False, task
=None):
23 :param include_http_status: Used for Tasks, where the HTTP status code is not available.
25 from ..tools
import ViewCache
26 if isinstance(e
, ViewCacheNoDataException
):
27 return {'status': ViewCache
.VALUE_NONE
, 'value': None}
29 out
= dict(detail
=str(e
))
32 except AttributeError:
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)
39 out
['task'] = dict(name
=task
.name
, metadata
=task
.metadata
) # type: ignore
43 # pylint: disable=broad-except
44 def dashboard_exception_handler(handler
, *args
, **kwargs
):
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
:
57 except Exception as error
:
58 logger
.exception('Internal Server Error')
63 def handle_cephfs_error():
66 except cephfs
.OSError as e
:
67 raise DashboardException(e
, component
='cephfs') from e
71 def handle_rbd_error():
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
__)
81 def handle_rados_error(component
):
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
__)
91 def handle_send_command_error(component
):
94 except SendCommandError
as e
:
95 raise DashboardException(e
, component
=component
)
99 def handle_orchestrator_error(component
):
102 except OrchestratorError
as e
:
103 raise DashboardException(e
, component
=component
)
107 def handle_request_error(component
):
110 except RequestException
as e
:
112 content
= json
.loads(e
.content
)
113 content_message
= content
.get('message')
115 raise DashboardException(
116 msg
=content_message
, component
=component
)
117 raise DashboardException(e
=e
, component
=component
)
121 def handle_error(component
, http_status_code
=None):
124 except Exception as e
: # pylint: disable=broad-except
125 raise DashboardException(e
, component
=component
, http_status_code
=http_status_code
)