[PATCH RFC 19/32] python//qmp.py: add QMPProtocolError

John Snow posted 32 patches 5 years, 6 months ago
Maintainers: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>, "Philippe Mathieu-Daudé" <philmd@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Fam Zheng <fam@euphon.net>, Eduardo Habkost <ehabkost@redhat.com>, Kevin Wolf <kwolf@redhat.com>, Markus Armbruster <armbru@redhat.com>, Max Reitz <mreitz@redhat.com>, Cleber Rosa <crosa@redhat.com>
[PATCH RFC 19/32] python//qmp.py: add QMPProtocolError
Posted by John Snow 5 years, 6 months ago
In the case that we receive a reply but are unable to understand it, use
this exception name to indicate that case.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 python/qemu/lib/qmp.py | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/python/qemu/lib/qmp.py b/python/qemu/lib/qmp.py
index e460234f2e..5fb16f4b42 100644
--- a/python/qemu/lib/qmp.py
+++ b/python/qemu/lib/qmp.py
@@ -62,6 +62,12 @@ class QMPTimeoutError(QMPError):
     """
 
 
+class QMPProtocolError(QMPError):
+    """
+    QMP protocol error; unexpected response
+    """
+
+
 class QMPResponseError(QMPError):
     """
     Represents erroneous QMP monitor reply
@@ -265,6 +271,10 @@ def command(self, cmd, **kwds):
         ret = self.cmd(cmd, kwds)
         if 'error' in ret:
             raise QMPResponseError(ret)
+        if 'return' not in ret:
+            raise QMPProtocolError(
+                "'return' key not found in QMP response '{}'".format(str(ret))
+            )
         return cast(QMPReturnValue, ret['return'])
 
     def pull_event(self, wait=False):
-- 
2.21.1


Re: [PATCH RFC 19/32] python//qmp.py: add QMPProtocolError
Posted by Philippe Mathieu-Daudé 5 years, 6 months ago
On 5/14/20 7:53 AM, John Snow wrote:
> In the case that we receive a reply but are unable to understand it, use
> this exception name to indicate that case.
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>   python/qemu/lib/qmp.py | 10 ++++++++++
>   1 file changed, 10 insertions(+)
> 
> diff --git a/python/qemu/lib/qmp.py b/python/qemu/lib/qmp.py
> index e460234f2e..5fb16f4b42 100644
> --- a/python/qemu/lib/qmp.py
> +++ b/python/qemu/lib/qmp.py
> @@ -62,6 +62,12 @@ class QMPTimeoutError(QMPError):
>       """
>   
>   
> +class QMPProtocolError(QMPError):
> +    """
> +    QMP protocol error; unexpected response
> +    """
> +
> +
>   class QMPResponseError(QMPError):
>       """
>       Represents erroneous QMP monitor reply
> @@ -265,6 +271,10 @@ def command(self, cmd, **kwds):
>           ret = self.cmd(cmd, kwds)
>           if 'error' in ret:
>               raise QMPResponseError(ret)
> +        if 'return' not in ret:
> +            raise QMPProtocolError(
> +                "'return' key not found in QMP response '{}'".format(str(ret))
> +            )
>           return cast(QMPReturnValue, ret['return'])
>   
>       def pull_event(self, wait=False):
> 

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>