Coverage for drivers/xs_errors.py : 96%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (C) Citrix Systems Inc.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU Lesser General Public License as published
5# by the Free Software Foundation; version 2.1 only.
6#
7# This program is distributed in the hope that it will be useful,
8# but WITHOUT ANY WARRANTY; without even the implied warranty of
9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10# GNU Lesser General Public License for more details.
11#
12# You should have received a copy of the GNU Lesser General Public License
13# along with this program; if not, write to the Free Software Foundation, Inc.,
14# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15#
16# Xensource error codes
17#
19import errno
20import os
21import xml.dom.minidom
22import util
23import xmlrpc.client
25DEF_LOC = os.path.dirname(__file__)
26XML_DEFS = os.path.join(DEF_LOC, 'XE_SR_ERRORCODES.xml')
29class SRException(Exception):
30 """Exception raised by storage repository operations"""
31 errno = errno.EINVAL
33 def __init__(self, reason):
34 Exception.__init__(self, reason)
36 def toxml(self):
37 return xmlrpc.client.dumps(xmlrpc.client.Fault(
38 int(self.errno), str(self)), "", True)
41class SROSError(SRException):
42 """Wrapper for OSError"""
44 def __init__(self, errno, reason):
45 self.errno = errno
46 Exception.__init__(self, reason)
49class XenError(Exception):
50 def __new__(self, key, opterr=None):
51 # Check the XML definition file exists
52 if not os.path.exists(XML_DEFS):
53 raise Exception("No XML def file found")
55 # Read the definition list
56 errorlist = self._fromxml('SM-errorcodes')
58 ########DEBUG#######
59 #for val in self.errorlist.keys():
60 # subdict = self.errorlist[val]
61 # print "KEY [%s]" % val
62 # for subval in subdict.keys():
63 # print "\tSUBKEY: %s, VALUE: %s" % (subval,subdict[subval])
64 ########END#######
66 # Now find the specific error
67 if key in errorlist: 67 ↛ 78line 67 didn't jump to line 78, because the condition on line 67 was never false
68 subdict = errorlist[key]
69 errorcode = int(subdict['value'])
70 errormessage = subdict['description']
71 if opterr is not None:
72 errormessage += " [opterr=%s]" % opterr
73 util.SMlog("Raising exception [%d, %s]" %
74 (errorcode, errormessage))
75 return SROSError(errorcode, errormessage)
77 # development error
78 return SROSError(1, "Error reporting error, unknown key %s" % key)
80 @staticmethod
81 def _fromxml(tag):
82 dom = xml.dom.minidom.parse(XML_DEFS)
83 objectlist = dom.getElementsByTagName(tag)[0]
85 errorlist = {}
86 for node in objectlist.childNodes:
87 taglist = {}
88 newval = False
89 for n in node.childNodes:
90 if n.nodeType == n.ELEMENT_NODE and node.nodeName == 'code':
91 taglist[n.nodeName] = ""
92 for e in n.childNodes:
93 if e.nodeType == e.TEXT_NODE: 93 ↛ 92line 93 didn't jump to line 92, because the condition on line 93 was never false
94 newval = True
95 taglist[n.nodeName] += e.data
96 if newval:
97 name = taglist['name']
98 errorlist[name] = taglist
99 return errorlist