Coverage for libs/sm/core/xs_errors.py: 99%

50 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2026-07-10 07:59 +0000

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# 

18 

19import errno 

20import os 

21import xml.dom.minidom 

22import xmlrpc.client 

23 

24XML_DEFS = '/usr/share/sm/XE_SR_ERRORCODES.xml' 

25 

26 

27class SRException(Exception): 

28 """Exception raised by storage repository operations""" 

29 errno = errno.EINVAL 

30 

31 def __init__(self, reason): 

32 super().__init__(reason) 

33 

34 def toxml(self): 

35 return xmlrpc.client.dumps(xmlrpc.client.Fault( 

36 int(self.errno), str(self)), "", True) 

37 

38 

39class SROSError(SRException): 

40 """Class which looks valuely like OSError""" 

41 

42 def __init__(self, errno, reason): 

43 self.errno = errno 

44 self.reason = reason 

45 super().__init__(reason) 

46 

47 def message(self): 

48 return self.reason 

49 

50class XenError(Exception): 

51 def __new__(self, key, opterr=None): 

52 # Check the XML definition file exists 

53 if not os.path.exists(XML_DEFS): 

54 raise Exception(f"No XML def file found ({XML_DEFS})") 

55 

56 # Read the definition list 

57 errorlist = self._fromxml('SM-errorcodes') 

58 

59 # ########DEBUG####### 

60 #for val in self.errorlist.keys(): 

61 # subdict = self.errorlist[val] 

62 # print "KEY [%s]" % val 

63 # for subval in subdict.keys(): 

64 # print "\tSUBKEY: %s, VALUE: %s" % (subval,subdict[subval]) 

65 ########END####### 

66 

67 # Now find the specific error 

68 if key in errorlist: 

69 subdict = errorlist[key] 

70 errorcode = int(subdict['value']) 

71 errormessage = subdict['description'] 

72 if opterr is not None: 

73 errormessage += " [opterr=%s]" % opterr 

74 return SROSError(errorcode, errormessage) 

75 

76 # development error 

77 return SROSError(1, "Error reporting error, unknown key %s" % key) 

78 

79 @staticmethod 

80 def _fromxml(tag): 

81 dom = xml.dom.minidom.parse(XML_DEFS) 

82 objectlist = dom.getElementsByTagName(tag)[0] 

83 

84 errorlist = {} 

85 for node in objectlist.childNodes: 

86 taglist = {} 

87 newval = False 

88 for n in node.childNodes: 

89 if n.nodeType == n.ELEMENT_NODE and node.nodeName == 'code': 

90 taglist[n.nodeName] = "" 

91 for e in n.childNodes: 

92 if e.nodeType == e.TEXT_NODE: 92 ↛ 91line 92 didn't jump to line 91, because the condition on line 92 was never false

93 newval = True 

94 taglist[n.nodeName] += e.data 

95 if newval: 

96 name = taglist['name'] 

97 errorlist[name] = taglist 

98 return errorlist