From: Stefan Schantl Date: Thu, 25 Aug 2022 16:53:10 +0000 (+0200) Subject: Improve error handling. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=badff15954af240106af802e77d48474fa965d90;p=people%2Fstevee%2Fperl-ipset.git Improve error handling. Signed-off-by: Stefan Schantl --- diff --git a/IPSet.xs b/IPSet.xs index 024f5af..956a687 100644 --- 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;