]> 91.132.146.200 Git - bibliotheca-php.git/commitdiff
better try catch and exceptions
authorBanana <mail@bananas-playground.net>
Tue, 5 Jan 2021 11:00:31 +0000 (12:00 +0100)
committerBanana <mail@bananas-playground.net>
Tue, 5 Jan 2021 11:00:31 +0000 (12:00 +0100)
webclient/lib/managecollectionfields.class.php
webclient/lib/managecollections.class.php
webclient/lib/manageentry.class.php
webclient/lib/possessed.class.php

index 4dfa5d9e9f18ead423df2ec62f744ad32516729e..072aa09080d0d74628ff1dc736b924351e420fbd 100644 (file)
@@ -120,7 +120,7 @@ class ManageCollectionFields {
        }
 
        /**
-        * Simple comma seperated number string
+        * Simple comma separated number string
         *
         * @param string $string
         * @return bool
@@ -157,11 +157,13 @@ class ManageCollectionFields {
                }
 
                if(!empty($ids)) {
-                       $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
 
                        $queryStr1 = "DELETE FROM `".DB_PREFIX."_collection_fields_".$this->_collectionId."`
                                                WHERE `fk_field_id` NOT IN (".implode(",",$ids).")";
                        try {
+                               $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
                                $q1 = $this->_DB->query($queryStr1);
                                if($q1 !== false) {
                                        // https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
@@ -188,19 +190,21 @@ class ManageCollectionFields {
                                                        $alterQuery = $this->_DB->query($alterString);
                                                }
                                                if(!empty($_newColumns) && $alterQuery == false) {
-                                                       $this->_DB->rollback();
-                                               }
-                                               else {
-                                                       $this->_DB->commit();
-                                                       $ret = true;
+                                                       throw new Exception("Failed to insert alter the table.");
                                                }
                                        }
                                        else {
-                                               $this->_DB->rollback();
+                                               throw new Exception("Failed to insert the new fields.");
                                        }
                                }
+                               else {
+                                       throw new Exception("Failed to delete old fields.");
+                               }
+                               $this->_DB->commit();
+                               $ret = true;
                        }
                        catch (Exception $e) {
+                               $this->_DB->rollback();
                                error_log("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
                        }
                }
index 5e52fa007536e8bfd6bd268ba87e4a212aca3830..0100f3fb856a2290051edeac6676bb9fb2441fba 100644 (file)
@@ -203,8 +203,10 @@ class ManageCollections {
                if(!empty($data['name']) === true
                        && $this->_validNewCollectionName($data['name']) === true
                ) {
-                       $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
                        try {
+                               $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
                                $queryStr = "INSERT INTO `".DB_PREFIX."_collection`
                                                        SET `name` = '".$this->_DB->real_escape_string($data['name'])."',
                                                                `description` = '".$this->_DB->real_escape_string($data['description'])."',
@@ -512,11 +514,13 @@ class ManageCollections {
        private function _updateToolRelation($id,$tool) {
                $ret = false;
 
-               $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
                $queryStr = "DELETE FROM `".DB_PREFIX."_tool2collection`
                                                                WHERE `fk_collection_id` = '".$this->_DB->real_escape_string($id)."'";
                if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
                try {
+                       $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
                        $this->_DB->query($queryStr);
 
                        if(!empty($tool)) {
index 2e4661e47128324da48ba312d7df25f888ebdd18..78910e8e0541cba71adb92a54b825ecfefeb5857 100644 (file)
@@ -187,7 +187,7 @@ class Manageentry {
                        if(DEBUG) error_log("[DEBUG] ".__METHOD__." queryData: ".var_export($queryData,true));
 
                        if(!empty($queryData['init'])) {
-                               $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
 
                                $queryStr = "INSERT INTO `".DB_PREFIX."_collection_entry_".$this->_collectionId."`";
                                if($update !== false && is_numeric($update)) {
@@ -206,6 +206,8 @@ class Manageentry {
                                if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
 
                                try {
+                                       $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
                                        $this->_DB->query($queryStr);
 
                                        if($update !== false && is_numeric($update)) {
@@ -227,13 +229,13 @@ class Manageentry {
                                                                $this->_runAfter_upload($q, $newId);
                                                        }
                                                }
-
-                                               $this->_DB->commit();
-                                               $ret = $newId;
                                        }
                                        else {
-                                               $this->_DB->rollback();
+                                               throw new Exception('Failed to create entry');
                                        }
+
+                                       $ret = $newId;
+                                       $this->_DB->commit();
                                }
                                catch (Exception $e) {
                                        $this->_DB->rollback();
@@ -261,10 +263,9 @@ class Manageentry {
                if(!empty($entryId) && !empty($this->_collectionId)) {
 
                        if ($this->_canDelete($entryId)) {
-
-                               $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
-
                                try {
+                                       $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
                                        // remove assets
                                        $_path = PATH_STORAGE.'/'.$this->_collectionId.'/'.$entryId;
                                        if(is_dir($_path) && is_readable($_path)) {
index 14c996aa327661a8bfb3d2c65a406904b67e14e8..9f93172fe441a3916c7eb86e90cf15bb0621effe 100644 (file)
@@ -116,7 +116,6 @@ class Possessed {
                        } else {
                                $active = "0";
                        }
-                       $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
 
                        $_password = password_hash($password, PASSWORD_DEFAULT);
 
@@ -131,6 +130,8 @@ class Possessed {
                                                        `group` = '".$this->_DB->real_escape_string($group)."'";
                        if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
                        try {
+                               $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
                                $query = $this->_DB->query($queryStr);
 
                                if ($query !== false) {
@@ -141,20 +142,18 @@ class Possessed {
                                        if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStrOwner,true));
                                        $this->_DB->query($queryStrOwner);
                                        $_setGroupRelation = $this->_setGroupReleation($_userid,$group);
-                                       if($_setGroupRelation !== false) {
-                                               $this->_DB->commit();
-                                               $ret = true;
-                                       }
-                                       else {
-                                               $this->_DB->rollback();
-                                               error_log('ERROR Failed to insert user relation: '.var_export($queryStr, true));
+                                       if($_setGroupRelation === false) {
+                                               throw new Exception("Failed to insert user relation");
                                        }
                                } else {
-                                       $this->_DB->rollback();
-                                       error_log('ERROR Failed to insert user: '.var_export($queryStr, true));
+                                       throw new Exception("Failed to insert user");
                                }
+
+                               $this->_DB->commit();
+                               $ret = true;
                        }
                        catch (Exception $e) {
+                               $this->_DB->rollback();
                                error_log("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
                        }
                }
@@ -190,7 +189,7 @@ class Possessed {
 
                        $_password = password_hash($password, PASSWORD_DEFAULT);
 
-                       $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
 
                        $queryStr = "UPDATE `".DB_PREFIX . "_user`
                                                SET `name` = '".$this->_DB->real_escape_string($username)."',
@@ -208,24 +207,23 @@ class Possessed {
                                                AND `protected` = '0'";
                        if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
                        try {
+                               $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
+
                                $query = $this->_DB->query($queryStr);
 
                                if ($query !== false) {
                                        $_setGroupRelation = $this->_setGroupReleation($id,$group, true);
-                                       if($_setGroupRelation !== false) {
-                                               $this->_DB->commit();
-                                               $ret = true;
-                                       }
-                                       else {
-                                               $this->_DB->rollback();
-                                               error_log('ERROR Failed to insert user relation: '.var_export($queryStr, true));
+                                       if($_setGroupRelation === false) {
+                                               throw new Exception('Failed to insert user relation');
                                        }
                                } else {
-                                       $this->_DB->rollback();
-                                       error_log('ERROR Failed to insert user: '.var_export($queryStr, true));
+                                       throw new Exception('Failed to insert user');
                                }
+                               $this->_DB->commit();
+                               $ret = true;
                        }
                        catch (Exception $e) {
+                               $this->_DB->rollback();
                                error_log("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
                        }
                }
@@ -273,24 +271,22 @@ class Possessed {
                $ret = false;
 
                if(!empty($id)) {
-                       $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
-
                        try {
+                               $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
                                $d1 = $this->_DB->query("DELETE FROM `".DB_PREFIX."_user` 
                                        WHERE `id` = '".$this->_DB->real_escape_string($id)."'
                                        AND `protected` = '0'");
                                $d2 = $this->_DB->query("DELETE FROM `".DB_PREFIX."_user2group` WHERE `fk_user_id` = '".$this->_DB->real_escape_string($id)."'");
                                $d3 = $this->_DB->query("DELETE FROM `".DB_PREFIX."_userSession` WHERE `fk_user_id` = '".$this->_DB->real_escape_string($id)."'");
 
-                               if($d1 !== false && $d2 !== false && $d3 !== false) {
-                                       $this->_DB->commit();
-                                       $ret = true;
-                               }
-                               else {
-                                       $this->_DB->rollback();
+                               if($d1 === false || $d2 === false || $d3 === false) {
+                                       throw new Exception('Failed to delete the user');
                                }
+                               $this->_DB->commit();
+                               $ret = true;
                        }
                        catch (Exception $e) {
+                               $this->_DB->rollback();
                                error_log("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
                        }
                }