ia64/xen-unstable

changeset 9427:da24df1ea484

Improve error handling, in particular fixing the ProtocolError that is thrown
when a domain is specified by the user that does not exist. Added a few
error codes -- many more to come, I expect.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Thu Mar 23 12:59:43 2006 +0100 (2006-03-23)
parents c1bb4eb56529
children 951ed0cd0480
files tools/python/xen/xend/XendClient.py tools/python/xen/xend/XendError.py tools/python/xen/xend/server/XMLRPCServer.py tools/python/xen/xm/main.py
line diff
     1.1 --- a/tools/python/xen/xend/XendClient.py	Thu Mar 23 11:58:19 2006 +0100
     1.2 +++ b/tools/python/xen/xend/XendClient.py	Thu Mar 23 12:59:43 2006 +0100
     1.3 @@ -21,4 +21,8 @@ from xen.util.xmlrpclib2 import ServerPr
     1.4  
     1.5  XML_RPC_SOCKET = "/var/run/xend-xmlrpc.sock"
     1.6  
     1.7 +ERROR_INTERNAL = 1
     1.8 +ERROR_GENERIC = 2
     1.9 +ERROR_INVALID_DOMAIN = 3
    1.10 +
    1.11  server = ServerProxy('httpu:///var/run/xend-xmlrpc.sock')
     2.1 --- a/tools/python/xen/xend/XendError.py	Thu Mar 23 11:58:19 2006 +0100
     2.2 +++ b/tools/python/xen/xend/XendError.py	Thu Mar 23 12:59:43 2006 +0100
     2.3 @@ -17,10 +17,12 @@
     2.4  
     2.5  from xmlrpclib import Fault
     2.6  
     2.7 +import XendClient
     2.8 +
     2.9  class XendError(Fault):
    2.10      
    2.11      def __init__(self, value):
    2.12 -        Fault.__init__(self, 2, value)
    2.13 +        Fault.__init__(self, XendClient.ERROR_GENERIC, value)
    2.14          self.value = value
    2.15  
    2.16      def __str__(self):
     3.1 --- a/tools/python/xen/xend/server/XMLRPCServer.py	Thu Mar 23 11:58:19 2006 +0100
     3.2 +++ b/tools/python/xen/xend/server/XMLRPCServer.py	Thu Mar 23 12:59:43 2006 +0100
     3.3 @@ -13,25 +13,45 @@
     3.4  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     3.5  #============================================================================
     3.6  # Copyright (C) 2006 Anthony Liguori <aliguori@us.ibm.com>
     3.7 -# Copyright (C) 2006 XenSource Ltd
     3.8 +# Copyright (C) 2006 XenSource Ltd.
     3.9  #============================================================================
    3.10  
    3.11 +import xmlrpclib
    3.12 +
    3.13  from xen.xend import XendDomain, XendDomainInfo, XendNode, \
    3.14                       XendLogging, XendDmesg
    3.15  from xen.util.xmlrpclib2 import UnixXMLRPCServer, TCPXMLRPCServer
    3.16  
    3.17 -from xen.xend.XendClient import XML_RPC_SOCKET
    3.18 +from xen.xend.XendClient import XML_RPC_SOCKET, ERROR_INVALID_DOMAIN
    3.19  
    3.20  def lookup(domid):
    3.21 -    return XendDomain.instance().domain_lookup_by_name_or_id(domid)
    3.22 +    try:
    3.23 +        return XendDomain.instance().domain_lookup_by_name_or_id(domid)
    3.24 +    except exn:
    3.25 +        log.exception(exn)
    3.26 +        raise exn
    3.27  
    3.28  def dispatch(domid, fn, args):
    3.29      info = lookup(domid)
    3.30 -    return getattr(info, fn)(*args)
    3.31 +    if info:
    3.32 +        try:
    3.33 +            return getattr(info, fn)(*args)
    3.34 +        except exn:
    3.35 +            log.exception(exn)
    3.36 +            raise exn
    3.37 +    else:
    3.38 +        raise xmlrpclib.Fault(ERROR_INVALID_DOMAIN, domid)
    3.39  
    3.40  def domain(domid):
    3.41      info = lookup(domid)
    3.42 -    return info.sxpr()
    3.43 +    if info:
    3.44 +        try:
    3.45 +            return info.sxpr()
    3.46 +        except exn:
    3.47 +            log.exception(exn)
    3.48 +            raise exn
    3.49 +    else:
    3.50 +        raise xmlrpclib.Fault(ERROR_INVALID_DOMAIN, domid)
    3.51  
    3.52  def domains(detail=1):
    3.53      if detail < 1:
     4.1 --- a/tools/python/xen/xm/main.py	Thu Mar 23 11:58:19 2006 +0100
     4.2 +++ b/tools/python/xen/xm/main.py	Thu Mar 23 12:59:43 2006 +0100
     4.3 @@ -1,6 +1,6 @@
     4.4  # (C) Copyright IBM Corp. 2005
     4.5  # Copyright (C) 2004 Mike Wray
     4.6 -# Copyright (c) 2005 XenSource Ltd
     4.7 +# Copyright (c) 2005-2006 XenSource Ltd.
     4.8  #
     4.9  # Authors:
    4.10  #     Sean Dague <sean at dague dot net>
    4.11 @@ -38,7 +38,7 @@ from xen.xend import sxp
    4.12  from xen.xm.opts import *
    4.13  
    4.14  import console
    4.15 -
    4.16 +import xen.xend.XendClient
    4.17  from xen.xend.XendClient import server
    4.18  
    4.19  # getopt.gnu_getopt is better, but only exists in Python 2.3+.  Use
    4.20 @@ -1111,7 +1111,10 @@ def main(argv=sys.argv):
    4.21          except SystemExit:
    4.22              sys.exit(1)
    4.23          except xmlrpclib.Fault, ex:
    4.24 -            print "Error: %s" % ex.faultString
    4.25 +            if ex.faultCode == xen.xend.XendClient.ERROR_INVALID_DOMAIN:
    4.26 +                print "Error: the domain '%s' does not exist." % ex.faultString
    4.27 +            else:
    4.28 +                print "Error: %s" % ex.faultString
    4.29              sys.exit(1)
    4.30          except:
    4.31              print "Unexpected error:", sys.exc_info()[0]