]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/services/exception.py
1 # -*- coding: utf-8 -*-
2 from __future__
import absolute_import
6 from contextlib
import contextmanager
11 from orchestrator
import OrchestratorError
13 from ..exceptions
import DashboardException
, ViewCacheNoDataException
14 from ..services
.ceph_service
import SendCommandError
16 logger
= logging
.getLogger('exception')
19 def serialize_dashboard_exception(e
, include_http_status
=False, task
=None):
22 :param include_http_status: Used for Tasks, where the HTTP status code is not available.
24 from ..tools
import ViewCache
25 if isinstance(e
, ViewCacheNoDataException
):
26 return {'status': ViewCache
.VALUE_NONE
, 'value': None}
28 out
= dict(detail
=str(e
))
31 except AttributeError:
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)
38 out
['task'] = dict(name
=task
.name
, metadata
=task
.metadata
) # type: ignore
42 # pylint: disable=broad-except
43 def dashboard_exception_handler(handler
, *args
, **kwargs
):
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')
59 def handle_rbd_error():
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
__)
69 def handle_rados_error(component
):
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
__)
79 def handle_send_command_error(component
):
82 except SendCommandError
as e
:
83 raise DashboardException(e
, component
=component
)
87 def handle_orchestrator_error(component
):
90 except OrchestratorError
as e
:
91 raise DashboardException(e
, component
=component
)