Repository: SINTEF/dlite (language: python)

Issue:
No identification of error in datamodel
If there is an error in the datamodel, the error reported is that is cannot find it. 
This is very confusing, because it does acutally find it but it cannot instantiate it. 
There should be an error saying "Cannot find it" and one that is "Can find the uri, but cannot instantiate it"
or similar.


Discussion / hints:
An example. If you do not have need for "dimensions" (all dims are 1), then it is still needed to have an empty list for "dimensions". If it is missing, the error returned is :   

File "/usr/local/lib/python3.9/site-packages/dlite/dlite.py", line 1512, in get_instance
    raise DLiteError(f"no such instance: {id}")
    
   The error is actually that 'dimensions" was not added in the datamodel itself.

Fix patch:
diff --git a/src/dlite-json.c b/src/dlite-json.c
index 9ffa222ba..509981785 100644
--- a/src/dlite-json.c
+++ b/src/dlite-json.c
@@ -494,9 +494,8 @@ static DLiteInstance *parse_instance(const char *src, jsmntok_t *obj,
     if (dlite_get_uuid(uuid2, id) < 0) goto fail;
     if (strcmp(uuid, uuid2) != 0)
       FAIL3("instance has id \"%s\", expected \"%s\" (%s)", uuid, uuid2, id);
-  } else {
-    id = (uri) ? uri : uuid;
   }
+  if (uri) id = uri;
 
   /* Get metadata */
   if ((item = jsmn_item(src, obj, "meta"))) {
@@ -524,8 +523,8 @@ static DLiteInstance *parse_instance(const char *src, jsmntok_t *obj,
     if ((t = jsmn_item(src, obj, "properties"))) dims[n++] = t->size;
     if ((t = jsmn_item(src, obj, "relations")))  dims[n++] = t->size;
     if (n != meta->_ndimensions)
-      FAIL3("expected %d dimensions, got %d: %s",
-            (int)meta->_ndimensions, (int)n, id);
+      FAIL1("metadata does not confirm to schema, please check dimensions, "
+            "properties and/or relations: %s", id);
 
   } else {
     if (meta->_ndimensions > 0) {


Test patch:
diff --git a/bindings/python/tests/test_entity.py b/bindings/python/tests/test_entity.py
index 245616f0c..ce05110c5 100755
--- a/bindings/python/tests/test_entity.py
+++ b/bindings/python/tests/test_entity.py
@@ -138,6 +138,29 @@
     print('*** catched error loading "non-existing-path..."')
 
 
+# Test for issue #352 - improved error message for missing dimensions
+json_repr = """
+{
+  "uri": "http://onto-ns.com/ex/0.1/test",
+  "properties": {
+    "x": {
+      "type": "int"
+    }
+  }
+}
+"""
+try:
+    entity = dlite.Instance.from_json(json_repr)
+except dlite.DLiteError as exc:
+    assert str(exc) == (
+        "Error 1: metadata does not confirm to schema, please check "
+        "dimensions, properties and/or relations: "
+        "http://onto-ns.com/ex/0.1/test"
+    )
+else:
+    assert False  # missing dimensions should raise an exception
+
+
 # Check pickling
 s = pickle.dumps(inst)
 inst3 = pickle.loads(s)