qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Lukáš Doktor" <ldoktor@redhat.com>
To: Eduardo Habkost <ehabkost@redhat.com>
Cc: apahim@redhat.com, qemu-devel@nongnu.org, famz@redhat.com,
	armbru@redhat.com, mreitz@redhat.com
Subject: Re: [Qemu-devel] [PATCH 05/11] qemu.py: Use custom exceptions rather than Exception
Date: Fri, 21 Jul 2017 08:37:34 +0200	[thread overview]
Message-ID: <6701dad4-40a5-7287-a4b8-a22e19b90ce2@redhat.com> (raw)
In-Reply-To: <20170720182753.GV2757@localhost.localdomain>

[-- Attachment #1: Type: text/plain, Size: 2748 bytes --]

Dne 20.7.2017 v 20:27 Eduardo Habkost napsal(a):
> On Thu, Jul 20, 2017 at 06:28:09PM +0200, Lukáš Doktor wrote:
>> The naked Exception should not be widely used. It makes sense to be a
>> bit more specific and use better-suited custom exceptions. As a benefit
>> we can store the full reply in the exception in case someone needs it
>> when catching the exception.
>>
>> Signed-off-by: Lukáš Doktor <ldoktor@redhat.com>
>> ---
>>  scripts/qemu.py | 17 +++++++++++++++--
>>  1 file changed, 15 insertions(+), 2 deletions(-)
>>
>> diff --git a/scripts/qemu.py b/scripts/qemu.py
>> index 5948e19..2bd522f 100644
>> --- a/scripts/qemu.py
>> +++ b/scripts/qemu.py
>> @@ -19,6 +19,19 @@ import subprocess
>>  import qmp.qmp
>>  
>>  
>> +class MonitorResponseError(qmp.qmp.QMPError):
>> +    '''
>> +    Represents erroneous QMP monitor reply
>> +    '''
>> +    def __init__(self, reply):
>> +        try:
>> +            desc = reply["error"]["desc"]
>> +        except KeyError:
>> +            desc = reply
>> +        super(MonitorResponseError, self).__init__(desc)
>> +        self.reply = reply
> 
> This would require every user of self.reply to first check if
> it's a string or dictionary.  All because of the "Monitor is
> closed" case below:
> 
I haven't used it for the `Monitor is closed` exception, so it's just to be able to store really broken reply safely. Anyway people can check whether the reply is a dict, or I can add `is_valid_reply` property which would check for `[error][desc]` presence (which is a bit more precise than just plain dict type checking).

>> +
>> +
>>  class QEMUMachine(object):
>>      '''A QEMU VM'''
>>  
>> @@ -197,9 +210,9 @@ class QEMUMachine(object):
>>          '''
>>          reply = self.qmp(cmd, conv_keys, **args)
>>          if reply is None:
>> -            raise Exception("Monitor is closed")
>> +            raise qmp.qmp.QMPError("Monitor is closed")
> 
> Should we really use the same exception type for this, if it's
> not really a QMP monitor error reply, and won't even have a real
> QMP reply in self.reply?
> 
I wasn't sure but the same exception can be caused by other failures when obtaining replies so I think in case the monitor is closed we might expect the same exception. Would importing it in the top level of this module to become `qemu.QMPError` work for you better, or would you prefer IOError, RuntimeError or another custom exception?

Lukáš

> 
>>          if "error" in reply:
>> -            raise Exception(reply["error"]["desc"])
>> +            raise MonitorResponseError(reply)
>>          return reply["return"]
>>  
>>      def get_qmp_event(self, wait=False):
>> -- 
>> 2.9.4
>>
> 



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 502 bytes --]

  reply	other threads:[~2017-07-21  6:37 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-20 16:28 [Qemu-devel] [PATCH 00/11] qemu.py: Pylint/style fixes Lukáš Doktor
2017-07-20 16:28 ` [Qemu-devel] [PATCH 01/11] " Lukáš Doktor
2017-07-21  5:28   ` Philipp Hahn
2017-07-20 16:28 ` [Qemu-devel] [PATCH 02/11] qemu.py: Avoid dangerous arguments Lukáš Doktor
2017-07-20 18:19   ` Eduardo Habkost
2017-07-22  1:22   ` Philippe Mathieu-Daudé
2017-07-20 16:28 ` [Qemu-devel] [PATCH 03/11] qemu.py: Use iteritems rather than keys() Lukáš Doktor
2017-07-20 18:19   ` Eduardo Habkost
2017-07-22  1:22   ` Philippe Mathieu-Daudé
2017-07-20 16:28 ` [Qemu-devel] [PATCH 04/11] qemu.py: Simplify QMP key-conversion Lukáš Doktor
2017-07-20 18:20   ` Eduardo Habkost
2017-07-20 16:28 ` [Qemu-devel] [PATCH 05/11] qemu.py: Use custom exceptions rather than Exception Lukáš Doktor
2017-07-20 18:27   ` Eduardo Habkost
2017-07-21  6:37     ` Lukáš Doktor [this message]
2017-07-21 18:42       ` Eduardo Habkost
2017-07-24 12:13         ` Lukáš Doktor
2017-07-24 15:32           ` Eduardo Habkost
2017-07-25  5:34             ` Lukáš Doktor
2017-07-20 16:28 ` [Qemu-devel] [PATCH 06/11] qmp.py: Couple of pylint/style fixes Lukáš Doktor
2017-07-22  1:30   ` Philippe Mathieu-Daudé
2017-07-24 12:36     ` Lukáš Doktor
2017-07-25  6:04       ` Philippe Mathieu-Daudé
2017-07-25  6:13         ` Lukáš Doktor
2017-07-25  6:20           ` Philippe Mathieu-Daudé
2017-07-20 16:28 ` [Qemu-devel] [PATCH 07/11] qmp.py: Use object-based class for QEMUMonitorProtocol Lukáš Doktor
2017-07-20 18:35   ` Eduardo Habkost
2017-07-21  6:50     ` Lukáš Doktor
2017-07-21 18:43       ` Eduardo Habkost
2017-07-20 16:28 ` [Qemu-devel] [PATCH 08/11] qmp.py: Avoid "has_key" usage Lukáš Doktor
2017-07-20 18:35   ` Eduardo Habkost
2017-07-22  1:31   ` Philippe Mathieu-Daudé
2017-07-20 16:28 ` [Qemu-devel] [PATCH 09/11] qmp.py: Avoid overriding a builtin object Lukáš Doktor
2017-07-20 18:38   ` Eduardo Habkost
2017-07-21  6:53     ` Lukáš Doktor
2017-07-21 18:46       ` Eduardo Habkost
2017-07-24 12:41         ` Lukáš Doktor
2017-07-20 16:28 ` [Qemu-devel] [PATCH 10/11] qtest.py: Few pylint/style fixes Lukáš Doktor
2017-07-20 18:42   ` Eduardo Habkost
2017-07-21  6:57     ` Lukáš Doktor
2017-07-21 18:56       ` Eduardo Habkost
2017-07-24 12:42         ` Lukáš Doktor
2017-07-20 16:28 ` [Qemu-devel] [PATCH 11/11] qtest.py: Avoid using mutable list as default argument Lukáš Doktor
2017-07-20 18:44   ` Eduardo Habkost
2017-07-21  6:58     ` Lukáš Doktor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6701dad4-40a5-7287-a4b8-a22e19b90ce2@redhat.com \
    --to=ldoktor@redhat.com \
    --cc=apahim@redhat.com \
    --cc=armbru@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=famz@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).