Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(xmlupload): prevent crash + improve error message when cardinalities are wrong (DEV-1559) #267

Merged
merged 1 commit into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions knora/dsplib/models/resource.py
Expand Up @@ -103,9 +103,9 @@ def __init__(self,
self._user_permission = user_permission

if self.baseclass in self.baseclasses_with_bitstream and bitstream is None:
raise BaseError(f"ERROR Baseclass '{self.baseclass}' requires a bitstream value!")
raise BaseError(f"ERROR in resource with label '{self._label}': Baseclass '{self.baseclass}' requires a bitstream value")
if self.baseclass not in self.baseclasses_with_bitstream and bitstream:
raise BaseError(f"ERROR Baseclass '{self.baseclass}' does not allow a bitstream value!")
raise BaseError(f"ERROR in resource with label '{self._label}': Baseclass '{self.baseclass}' does not allow a bitstream value")
if self.baseclass in self.baseclasses_with_bitstream and bitstream:
self._bitstream = bitstream
else:
Expand All @@ -124,7 +124,7 @@ def __init__(self,
for val in value:
# check if cardinality allows multiple values for a property
if cardinality == Cardinality.C_0_1 or cardinality == Cardinality.C_1:
raise BaseError(f"ERROR Ontology does not allow multiple values for '{property_name}'!")
raise BaseError(f"ERROR in resource with label '{self._label}': Ontology does not allow multiple values for '{property_name}'")

if type(val) is Value:
self._values[property_name].append(val)
Expand Down Expand Up @@ -156,11 +156,11 @@ def __init__(self,
self._values[property_name] = value_type(value)
else:
if cardinality == Cardinality.C_1 or cardinality == Cardinality.C_1_n:
raise BaseError(f"ERROR The ontology does require at least one value for '{property_name}'!")
raise BaseError(f"ERROR in resource with label '{self._label}': The ontology requires at least one value for '{property_name}'")

for property_name in values:
if property_name not in self.knora_properties and not self.properties.get(property_name):
raise BaseError(f"ERROR Property '{property_name}' is not part of ontology!")
raise BaseError(f"ERROR in resource with label '{self._label}': Property '{property_name}' is not part of ontology")

def value(self, item) -> Optional[list[Value]]:
if self._values.get(item):
Expand Down
18 changes: 9 additions & 9 deletions knora/dsplib/utils/xml_upload.py
Expand Up @@ -518,16 +518,16 @@ def _upload_resources(
# create the resource in DSP
resclass_type = resclass_name_2_type[resource.restype]
properties = resource.get_propvals(id2iri_mapping, permissions_lookup)
resource_instance: ResourceInstance = resclass_type(
con=con,
label=resource.label,
iri=resource_iri,
permissions=permissions_lookup.get(resource.permissions), # type: ignore
creation_date=resource.creation_date,
bitstream=resource_bitstream,
values=properties
)
try:
resource_instance: ResourceInstance = resclass_type(
con=con,
label=resource.label,
iri=resource_iri,
permissions=permissions_lookup.get(resource.permissions), # type: ignore
creation_date=resource.creation_date,
bitstream=resource_bitstream,
values=properties
)
resource_creation_start = datetime.now()
created_resource: ResourceInstance = try_network_action(
action=lambda: resource_instance.create(),
Expand Down