]> git.ipfire.org Git - people/stevee/perl-ipset.git/commitdiff
Improve error handling.
authorStefan Schantl <stefan.schantl@ipfire.org>
Thu, 25 Aug 2022 16:53:10 +0000 (18:53 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Thu, 25 Aug 2022 16:53:10 +0000 (18:53 +0200)
Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
IPSet.xs

index 024f5afda44dbdc582c46f3d09f964fde528338d..956a687c78cc675e6b903798943718ea0bdee9cd 100644 (file)
--- a/IPSet.xs
+++ b/IPSet.xs
@@ -57,62 +57,51 @@ bool create_set(session, setname, typename, hashsize, maxelem)
                // Assin the setname to the session data.
                int r = ipset_session_data_set(session, IPSET_SETNAME, setname);
                if (r < 0) {
-                       printf("Could not set ipset name: %s\n",
-                               ipset_session_report_msg(session));
-
-                       return;
+                       goto ERROR;
                }
 
                // Assign additinal options to the session data.
                r = ipset_parse_typename(session, IPSET_OPT_TYPE, typename);
                if (r < 0) {
-                       printf("Could not set set type: %s\n",
-                               ipset_session_report_msg(session));
-
-                       return;
+                       goto ERROR;
                }
                
                type = ipset_type_get(session, IPSET_CMD_CREATE);
                if (!type) {
-                       printf("Could not get hash type.\n");
-
-                       return;
+                       goto ERROR;
                }
 
                r = ipset_session_data_set(session, IPSET_OPT_FAMILY, &family);
                if (r < 0) {
-                       printf("Could not set family: %s\n",
-                               ipset_session_report_msg(session));
-
-                       return;
+                       goto ERROR;
                }
 
                r = ipset_session_data_set(session, IPSET_OPT_HASHSIZE, &hashsize);
                if (r < 0) {
-                       printf("Could not set hashsize: %s\n",
-                               ipset_session_report_msg(session));
-
-                       return;
+                       goto ERROR;
                }
 
                r = ipset_session_data_set(session, IPSET_OPT_MAXELEM, &maxelem);
                if (r < 0) {
-                       printf("Could not set maxelem: %s\n",
-                               ipset_session_report_msg(session));
-
-                       return;
+                       goto ERROR;
                }
 
                r = ipset_cmd(session, IPSET_CMD_CREATE, 0);
                if (r < 0) {
-                       printf("Command failed: %s\n",
-                               ipset_session_report_msg(session));
-
-                       RETVAL = false;
+                       goto ERROR;
                }
-                       
+
                RETVAL = true;
 
+               goto END;
+
+               ERROR:
+                       RETVAL = false;
+
+                       // Reset may assigned session data.
+                       ipset_data_reset(ipset_session_data(session));
+
+               END:
        OUTPUT:
                RETVAL
        
@@ -129,20 +118,25 @@ delete_set(session, setname)
                // Assign the setname to the session data.
                int r = ipset_session_data_set(session, IPSET_SETNAME, setname);
                if (r < 0) {
-                       printf("Could not set setname: %s\n",
-                               ipset_session_report_msg(session));
+                       goto ERROR;
                }
 
                r = ipset_cmd(session, cmd, 0);
                if (r < 0) {
-                       printf("Command failed: %s\n",
-                               ipset_session_report_msg(session));
-
-                       RETVAL = false;
+                       goto ERROR;
                }
 
                RETVAL = true;
 
+               goto END;
+
+               ERROR:
+                       RETVAL = false;
+
+                       // Reset session data.
+                       ipset_data_reset(ipset_session_data(session));
+
+               END:
        OUTPUT:
                RETVAL
 
@@ -159,26 +153,29 @@ rename_set(session, setname, new_setname)
                // Assign the setname to the session data.
                int r = ipset_session_data_set(session, IPSET_SETNAME, setname);
                if (r < 0) {
-                       printf("Could not set setname: %s\n",
-                               ipset_session_report_msg(session));
+                       goto ERROR;
                }
 
                r = ipset_session_data_set(session, IPSET_OPT_SETNAME2, new_setname);
                if (r < 0) {
-                       printf("Could not assign new setname: %s\n",
-                               ipset_session_report_msg(session));
+                       goto ERROR;
                }
 
                r = ipset_cmd(session, cmd, 0);
                if (r < 0) {
-                       printf("Command failed: %s\n",
-                               ipset_session_report_msg(session));
-
-                       RETVAL = false;
+                       goto ERROR;
                }
 
                RETVAL = true;
 
+               goto END;
+
+               ERROR:
+                       RETVAL = false;
+
+                       // Reset session data.
+                       ipset_data_reset(ipset_session_data(session));
+               END:
        OUTPUT:
                RETVAL
 
@@ -196,27 +193,30 @@ swap_set(session, setname, setname2)
                // Assign the setname to the session data.
                int r = ipset_session_data_set(session, IPSET_SETNAME, setname);
                if (r < 0) {
-                       printf("Could not set setname: %s\n",
-                               ipset_session_report_msg(session));
+                       goto ERROR;
                }
 
                // Assign the second set data.
                r = ipset_session_data_set(session, IPSET_OPT_SETNAME2, setname2);
                if (r < 0) {
-                       printf("Could not assign second set name: %s\n",
-                               ipset_session_report_msg(session));
+                       goto ERROR;
                }
 
                r = ipset_cmd(session, cmd, 0);
                if (r < 0) {
-                       printf("Command failed: %s\n",
-                               ipset_session_report_msg(session));
-
-                               RETVAL = false;
+                       goto ERROR;
                }
 
                RETVAL = true;
 
+               goto END;
+
+               ERROR:
+                       RETVAL = false;
+
+                       // Reset session data.
+                       ipset_data_reset(ipset_session_data(session));
+               END:
        OUTPUT:
                RETVAL
 
@@ -232,24 +232,28 @@ setname_exists(session, setname)
                // Assign the setname as session data.
                int r = ipset_session_data_set(session, IPSET_SETNAME, setname);
                if (r < 0) {
-                       printf("Could not set setname: %s\n",
-                               ipset_session_report_msg(session));
+                       goto ERROR;
                }
 
                r = ipset_cmd(session, cmd, 0);
                if (r < 0 ) {
-                       printf("Command failed: %s\n",
-                               ipset_session_report_msg(session));
-               
-                       RETVAL = false;
+                       goto ERROR;
                }
                
                RETVAL = true;
 
+               goto END;
+
+               ERROR:
+                       RETVAL = false;
+
+                       // Reset session data.
+                       ipset_data_reset(ipset_session_data(session));
+               END:
        OUTPUT:
                RETVAL
 
-       
+
 void
 DESTROY(session)
        struct ipset_session *session;