Commit e46d352b authored by olivier R-D's avatar olivier R-D

raise UAStatusCodeError from StatsuCode check, move errors definition to their own files

parent 19ce5407
"""
Define exceptions to be raised at various places in the stack
"""
class UAError(RuntimeError):
pass
class UAStatusCodeError(UAError):
pass
class UAStringParsingError(UAError):
pass
......@@ -11,8 +11,7 @@ except ImportError:
import trollius as asyncio
class UAError(RuntimeError):
pass
from opcua.common.uaerrors import UAError
class ServiceError(UAError):
......
#AUTOGENERATED!!!
from opcua.common.uaerrors import UAStatusCodeError
class StatusCodes(object):
Good = 0
......
......@@ -6,6 +6,7 @@ from datetime import datetime
from enum import Enum, IntEnum
from opcua.common.utils import Buffer
from opcua.common.uaerrors import UAError
from opcua.ua.uatypes import *
from opcua.ua.object_ids import ObjectIds
......
......@@ -13,7 +13,8 @@ if sys.version_info.major > 2:
unicode = str
from opcua.ua import status_codes
from opcua.common.utils import UAError
from opcua.common.uaerrors import UAError
from opcua.common.uaerrors import UAStatusCodeError
logger = logging.getLogger('opcua.uaprotocol')
......@@ -310,7 +311,7 @@ class StatusCode(FrozenClass):
use is is_good() method if not exception is desired
"""
if self.value != 0:
raise UAError("{}({})".format(self.doc, self.name))
raise UAStatusCodeError("{}({})".format(self.doc, self.name))
def is_good(self):
"""
......
......@@ -59,6 +59,7 @@ class CodeGenerator(object):
self.write("from enum import Enum, IntEnum")
self.write("")
self.write("from opcua.common.utils import Buffer")
self.write("from opcua.common.uaerrors import UAError")
self.write("from opcua.ua.uatypes import *")
self.write("from opcua.ua.object_ids import ObjectIds")
......
......@@ -22,6 +22,7 @@ if __name__ == "__main__":
outputfile = open("../opcua/ua/status_codes.py", "w")
outputfile.write("#AUTOGENERATED!!!\n")
outputfile.write("\n")
outputfile.write("from opcua.common.uaerrors import UAStatusCodeError\n")
#outputfile.write("from enum import Enum\n")
outputfile.write("\n")
......@@ -47,36 +48,6 @@ if __name__ == "__main__":
else:
return 'UnknownUaError', 'Unknown StatusCode value: {}'.format(val)
""")
'''
outputfile.write("class StatusCode(Enum):\n")
outputfile.write(" Good = 0\n")
for line in inputfile:
name, val, doc = line.split(",", maxsplit=2)
doc = doc.strip()
outputfile.write(" {} = {}\n".format(name, val))
outputfile.write("""
def __new__(self, value=0):
Enum.__new__(self, value)
def to_binary(self):
return struct.pack("!I", self.value)
@staticmethod
def from_binary(data):
val = struct.unpack("!I", data.read(4))[0]
sc = StatusCode(val)
return sc
def check(self):
if self.value.name != "Good":
raise Exception(self.name)
""")
#outputfile.write("\n")
#outputfile.write("\n")
#outputfile.write("def CheckStatusCode(statuscode):\n")
#outputfile.write(" if statuscode.data == {}\n")
'''
......@@ -17,10 +17,10 @@ def extensionobject_from_binary(data):
if TypeId.Identifier == 0:
return None
elif TypeId.Identifier not in ExtensionClasses:
raise Exception("unknown ExtensionObject Type: {}".format(TypeId))
raise UAError("unknown ExtensionObject Type: {}".format(TypeId))
klass = ExtensionClasses[TypeId.Identifier]
if body is None:
raise Exception("parsing ExtensionObject {} without data".format(klass.__name__))
raise UAError("parsing ExtensionObject {} without data".format(klass.__name__))
return klass.from_binary(body)
......
......@@ -386,11 +386,11 @@ class CommonTests(object):
def test_subscribe_events_to_wrong_node(self):
sub = self.opc.create_subscription(100, sclt)
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
handle = sub.subscribe_events(self.opc.get_node("i=85"))
o = self.opc.get_objects_node()
v = o.add_variable(3, 'VariableNoEventNofierAttribute', 4)
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
handle = sub.subscribe_events(v)
sub.delete()
......@@ -444,12 +444,12 @@ class CommonTests(object):
def test_non_existing_path(self):
root = self.opc.get_root_node()
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
server_time_node = root.get_child(['0:Objects', '0:Server', '0:nonexistingnode'])
def test_bad_attribute(self):
root = self.opc.get_root_node()
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
root.set_value(99)
def test_get_node_by_nodeid(self):
......@@ -578,7 +578,7 @@ class CommonTests(object):
def test_add_exception(self):
objects = self.opc.get_objects_node()
o = objects.add_object('ns=2;i=103;', '2:AddReadObject')
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
o2 = objects.add_object('ns=2;i=103;', '2:AddReadObject')
def test_negative_value(self):
......@@ -595,13 +595,13 @@ class CommonTests(object):
def test_bad_node(self):
bad = self.opc.get_node(ua.NodeId(999, 999))
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
bad.get_browse_name()
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
bad.set_value(89)
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
bad.add_object(0, "0:myobj")
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
bad.get_child("0:myobj")
def test_value(self):
......@@ -660,7 +660,7 @@ class CommonTests(object):
msclt = MySubHandler()
o = self.opc.get_objects_node()
sub = self.opc.create_subscription(100, msclt)
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
handle1 = sub.subscribe_data_change(o) # we can only subscribe to variables so this should fail
sub.delete()
......@@ -722,13 +722,13 @@ class CommonTests(object):
self.assertEqual(node, v1)
self.assertEqual(val, [5])
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
sub.unsubscribe(999) # non existing handle
sub.unsubscribe(handle1)
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
sub.unsubscribe(handle1) # second try should fail
sub.delete()
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
sub.unsubscribe(handle1) # sub does not exist anymore
def test_subscription_data_change(self):
......@@ -762,13 +762,13 @@ class CommonTests(object):
self.assertEqual(node, v1)
self.assertEqual(val, [5])
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
sub.unsubscribe(999) # non existing handle
sub.unsubscribe(handle1)
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
sub.unsubscribe(handle1) # second try should fail
sub.delete()
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
sub.unsubscribe(handle1) # sub does not exist anymore
......@@ -872,10 +872,10 @@ class CommonTests(object):
m = o.get_child("2:ServerMethod")
result = o.call_method("2:ServerMethod", 2.1)
self.assertEqual(result, 4.2)
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
# FIXME: we should raise a more precise exception
result = o.call_method("2:ServerMethod", 2.1, 89, 9)
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
result = o.call_method(ua.NodeId(999), 2.1) # non existing method
def test_method_array(self):
......@@ -965,14 +965,14 @@ class AdminTestClient(unittest.TestCase, CommonTests):
def test_service_fault(self):
request = ua.ReadRequest()
request.TypeId = ua.FourByteNodeId(999) # bad type!
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
self.clt.bclient._uasocket.send_request(request)
def test_objects_anonymous(self):
objects = self.ro_clt.get_objects_node()
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
objects.set_attribute(ua.AttributeIds.WriteMask, ua.DataValue(999))
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
f = objects.add_folder(3, 'MyFolder')
def test_folder_anonymous(self):
......@@ -980,7 +980,7 @@ class AdminTestClient(unittest.TestCase, CommonTests):
f = objects.add_folder(3, 'MyFolderRO')
f_ro = self.ro_clt.get_node(f.nodeid)
self.assertEqual(f, f_ro)
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
f2 = f_ro.add_folder(3, 'MyFolder2')
def test_variable_anonymous(self):
......@@ -988,14 +988,14 @@ class AdminTestClient(unittest.TestCase, CommonTests):
v = objects.add_variable(3, 'MyROVariable', 6)
v.set_value(4) #this should work
v_ro = self.ro_clt.get_node(v.nodeid)
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
v_ro.set_value(2)
self.assertEqual(v_ro.get_value(), 4)
v.set_writable(True)
v_ro.set_value(2) #now it should work
self.assertEqual(v_ro.get_value(), 2)
v.set_writable(False)
with self.assertRaises(ua.UAError):
with self.assertRaises(ua.UAStatusCodeError):
v_ro.set_value(9)
self.assertEqual(v_ro.get_value(), 2)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment