Skip to content

Commit e3a23b4

Browse files
jnsnowphilmd
authored andcommitted
python/qmp.py: re-absorb MonitorResponseError
When I initially split this out, I considered this more of a machine error than a QMP protocol error, but I think that's misguided. Move this back to qmp.py and name it QMPResponseError. Convert qmp.command() to use this exception type. Signed-off-by: John Snow <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Reviewed-by: Kevin Wolf <[email protected]> Message-Id: <[email protected]> Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
1 parent 2012453 commit e3a23b4

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

python/qemu/machine.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,6 @@ class AbnormalShutdown(QEMUMachineError):
5656
"""
5757

5858

59-
class MonitorResponseError(qmp.QMPError):
60-
"""
61-
Represents erroneous QMP monitor reply
62-
"""
63-
def __init__(self, reply):
64-
try:
65-
desc = reply["error"]["desc"]
66-
except KeyError:
67-
desc = reply
68-
super().__init__(desc)
69-
self.reply = reply
70-
71-
7259
class QEMUMachine:
7360
"""
7461
A QEMU VM
@@ -533,7 +520,7 @@ def command(self, cmd, conv_keys=True, **args):
533520
if reply is None:
534521
raise qmp.QMPError("Monitor is closed")
535522
if "error" in reply:
536-
raise MonitorResponseError(reply)
523+
raise qmp.QMPResponseError(reply)
537524
return reply["return"]
538525

539526
def get_qmp_event(self, wait=False):

python/qemu/qmp.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ class QMPTimeoutError(QMPError):
6161
"""
6262

6363

64+
class QMPResponseError(QMPError):
65+
"""
66+
Represents erroneous QMP monitor reply
67+
"""
68+
def __init__(self, reply: QMPMessage):
69+
try:
70+
desc = reply['error']['desc']
71+
except KeyError:
72+
desc = reply
73+
super().__init__(desc)
74+
self.reply = reply
75+
76+
6477
class QEMUMonitorProtocol:
6578
"""
6679
Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP) and then
@@ -251,8 +264,8 @@ def command(self, cmd, **kwds):
251264
Build and send a QMP command to the monitor, report errors if any
252265
"""
253266
ret = self.cmd(cmd, kwds)
254-
if "error" in ret:
255-
raise Exception(ret['error']['desc'])
267+
if 'error' in ret:
268+
raise QMPResponseError(ret)
256269
return ret['return']
257270

258271
def pull_event(self, wait=False):

scripts/render_block_graph.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
from graphviz import Digraph
2626

2727
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'python'))
28-
from qemu.machine import MonitorResponseError
28+
from qemu.qmp import (
29+
QEMUMonitorProtocol,
30+
QMPResponseError,
31+
)
2932

3033

3134
def perm(arr):
@@ -102,7 +105,7 @@ def command(self, cmd):
102105
reply = json.loads(subprocess.check_output(ar))
103106

104107
if 'error' in reply:
105-
raise MonitorResponseError(reply)
108+
raise QMPResponseError(reply)
106109

107110
return reply['return']
108111

0 commit comments

Comments
 (0)