python: Raise IndexError when adding the same network twice
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 30 Jan 2018 15:37:40 +0000 (15:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 30 Jan 2018 15:37:40 +0000 (15:37 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/network.c
src/python/writer.c

index 4f29714..3d38612 100644 (file)
@@ -511,7 +511,7 @@ LOC_EXPORT int loc_network_tree_add_network(struct loc_network_tree* tree, struc
        // Check if node has not been set before
        if (node->network) {
                DEBUG(tree->ctx, "There is already a network at this path\n");
-               return 1;
+               return -EBUSY;
        }
 
        // Point node to the network
index e0c37b6..a15f77e 100644 (file)
@@ -128,8 +128,15 @@ static PyObject* Writer_add_network(WriterObject* self, PyObject* args) {
        // Create network object
        int r = loc_writer_add_network(self->writer, &network, string);
        if (r) {
-               if (r == -EINVAL)
-                       PyErr_SetString(PyExc_ValueError, "Invalid network");
+               switch (r) {
+                       case -EINVAL:
+                               PyErr_SetString(PyExc_ValueError, "Invalid network");
+                               break;
+
+                       case -EBUSY:
+                               PyErr_SetString(PyExc_IndexError, "A network already exists here");
+                               break;
+               }
 
                return NULL;
        }