Skip to content
Snippets Groups Projects
Commit aa0d48a4 authored by Jan David Mol's avatar Jan David Mol
Browse files

L2SS-295: Use clearer formatting of strings.

parent 209430b3
Branches
Tags
1 merge request!72L2SS-295: Make some things more dev/user friendly
...@@ -227,18 +227,35 @@ class ProtocolAttribute: ...@@ -227,18 +227,35 @@ class ProtocolAttribute:
try: try:
self.node.set_data_value(opcua.ua.uatypes.Variant(value=value, varianttype=self.ua_type)) self.node.set_data_value(opcua.ua.uatypes.Variant(value=value, varianttype=self.ua_type))
raise TypeError
except (TypeError, opcua.ua.uaerrors.BadTypeMismatch) as e: except (TypeError, opcua.ua.uaerrors.BadTypeMismatch) as e:
# A type conversion went wrong or there is a type mismatch. # A type conversion went wrong or there is a type mismatch.
# #
# This is either the conversion us -> opcua in our client, or client -> server. # This is either the conversion us -> opcua in our client, or client -> server.
# Report all types involved to allow assessment of the location of the error. # Report all types involved to allow assessment of the location of the error.
if type(value) == list: if type(value) == list:
our_type = "list(%s) x (%d)" % (type(value[0]).__name__ if value else "", len(value)) our_type = "list({dtype}) x ({dimensions})".format(
dtype=(type(value[0]).__name__ if value else ""),
dimensions=len(value))
else: else:
our_type = "%s" % type(value) our_type = "{dtype}".format(
dtype=type(value))
is_scalar = (self.dim_x + self.dim_y) == 1 is_scalar = (self.dim_x + self.dim_y) == 1
expected_server_type = "%s %s" % (self.ua_type, "(scalar)" if is_scalar else "x (%d, %d)" % (self.dim_x, self.dim_y))
actual_server_type = "%s x %s" % (self.node.get_data_type_as_variant_type(), self.node.get_array_dimensions() or "???")
raise TypeError("Cannot write value to OPC-UA attribute '%s': tried to convert data type %s to expected server type %s, server reports type %s" % (self.node.get_display_name().to_string(), our_type, expected_server_type, actual_server_type)) from e if is_scalar:
expected_server_type = "{dtype} (scalar)".format(
dtype=self.ua_type)
else:
expected_server_type = "{dtype} x ({dim_x}, {dim_y})".format(
dtype=self.ua_type,
dim_x=self.dim_x,
dim_y=self.dim_y)
actual_server_type = "{dtype} {dimensions}".format(
dtype=self.node.get_data_type_as_variant_type(),
dimensions=(self.node.get_array_dimensions() or "???"))
attribute_name = self.node.get_display_name().to_string()
raise TypeError(f"Cannot write value to OPC-UA attribute '{attribute_name}': tried to convert data type {our_type} to expected server type {expected_server_type}, server reports type {actual_server_type}") from e
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment