Change exception type in db-crypto.cpp 88/52688/5
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Wed, 25 Nov 2015 15:52:45 +0000 (16:52 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 16 Dec 2015 11:22:20 +0000 (03:22 -0800)
Change-Id: I537d47a4d6cb4d632a46f0527be17108e1353b4a

src/manager/common/exception.h
src/manager/service/ckm-logic.cpp
src/manager/service/db-crypto.cpp
src/manager/service/db-crypto.h

index de4123a..2c9ffe9 100644 (file)
@@ -118,6 +118,16 @@ typedef DefineException<CKM_API_ERROR_FILE_SYSTEM,
         Stringify, PrintError> FileSystemFailed;
 typedef DefineException<CKM_API_ERROR_AUTHENTICATION_FAILED,
         StringifyDebug, PrintDebug> AuthenticationFailed;
+typedef DefineException<CKM_API_ERROR_DB_ERROR,
+        StringifyError, PrintError> DatabaseFailed;
+
+
+struct TransactionFailed : public DatabaseFailed {
+    template<typename... Args>
+    TransactionFailed(const char *path, const char *function, int line, const Args&... args)
+      : DatabaseFailed(path, function, line, args...)
+    {}
+};
 
 } // namespace Exc
 } // namespace CKM
index 2f2ee4b..8c967f3 100644 (file)
@@ -306,12 +306,6 @@ RawBuffer CKMLogic::removeApplicationData(const Label &smackLabel) {
             }
         }
 
-    } catch (const DB::Crypto::Exception::InternalError &e) {
-        LogError("DB::Crypto couldn't remove data: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
-    } catch (const DB::Crypto::Exception::TransactionError &e) {
-        LogError("DB::Crypto transaction failed with message " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
     } catch (const Exc::Exception &e) {
         retCode = e.error();
     } catch (const CKM::Exception &e) {
@@ -439,12 +433,6 @@ int CKMLogic::verifyAndSaveDataHelper(
         {
             retCode = saveDataHelper(cred, name, label, binaryData, policy);
         }
-    } catch (const DB::Crypto::Exception::InternalError &e) {
-        LogError("DB::Crypto failed with message: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
-    } catch (const DB::Crypto::Exception::TransactionError &e) {
-        LogError("DB::Crypto transaction failed with message " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
     } catch (const Exc::Exception &e) {
         retCode = e.error();
     } catch (const CKM::Exception &e) {
@@ -469,9 +457,6 @@ int CKMLogic::getKeyForService(
         if (retCode == CKM_API_SUCCESS)
             key = std::move(obj);
         return retCode;
-    } catch (const DB::Crypto::Exception::Base &e) {
-        LogError("DB::Crypto failed with message: " << e.GetMessage());
-        return CKM_API_ERROR_DB_ERROR;
     } catch (const Exc::Exception &e) {
         return e.error();
     } catch (const CKM::Exception &e) {
@@ -553,12 +538,6 @@ RawBuffer CKMLogic::savePKCS12(
         retCode = saveDataHelper(cred, name, label, pkcs, keyPolicy, certPolicy);
     } catch (const Exc::Exception &e) {
         retCode = e.error();
-    } catch (const DB::Crypto::Exception::InternalError &e) {
-        LogError("DB::Crypto failed with message: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
-    } catch (const DB::Crypto::Exception::TransactionError &e) {
-        LogError("DB::Crypto transaction failed with message " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
     } catch (const CKM::Exception &e) {
         LogError("CKM::Exception: " << e.GetMessage());
         retCode = CKM_API_ERROR_SERVER_ERROR;
@@ -644,9 +623,9 @@ RawBuffer CKMLogic::removeData(
     {
         retCode = e.error();
     }
-    catch (const CKM::Exception &)
+    catch (const CKM::Exception &e)
     {
-        LogError("Error in deleting row!");
+        LogError("Error: " << e.GetMessage());
         retCode = CKM_API_ERROR_DB_ERROR;
     }
 
@@ -904,9 +883,6 @@ RawBuffer CKMLogic::getData(
         retCode = readDataHelper(true, cred, dataType, name, label, password, obj, objDataType);
         if(retCode == CKM_API_SUCCESS)
             row.data = std::move(obj->getBinary());
-    } catch (const DB::Crypto::Exception::Base &e) {
-        LogError("DB::Crypto failed with message: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
     } catch (const Exc::Exception &e) {
         retCode = e.error();
     } catch (const CKM::Exception &e) {
@@ -989,9 +965,6 @@ RawBuffer CKMLogic::getPKCS12(
         // prepare response
         if(retCode == CKM_API_SUCCESS)
             output = PKCS12Serializable(privKey, cert, caChain);
-    } catch (const DB::Crypto::Exception::Base &e) {
-        LogError("DB::Crypto failed with message: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
     } catch (const Exc::Exception &e) {
         retCode = e.error();
     } catch (const CKM::Exception &e) {
@@ -1015,7 +988,7 @@ int CKMLogic::getDataListHelper(const Credentials &cred,
     {
         auto &database = m_userDataMap[cred.clientUid].database;
 
-        Try {
+        try {
             LabelNameVector tmpVector;
             if (dataType.isKey()) {
                 // list all key types
@@ -1031,10 +1004,11 @@ int CKMLogic::getDataListHelper(const Credentials &cred,
             }
             labelNameVector.insert(labelNameVector.end(), tmpVector.begin(), tmpVector.end());
             retCode = CKM_API_SUCCESS;
-        }
-        Catch (CKM::Exception) {
-            LogError("Failed to get names");
+        } catch (const CKM::Exception &e) {
+            LogError("Error: " << e.GetMessage());
             retCode = CKM_API_ERROR_DB_ERROR;
+        } catch (const Exc::Exception &e) {
+            retCode = e.error();
         }
     }
     return retCode;
@@ -1327,12 +1301,6 @@ RawBuffer CKMLogic::createKeyPair(
                         policyPublic);
     } catch(const Exc::Exception &e) {
         retCode = e.error();
-    } catch (DB::Crypto::Exception::TransactionError &e) {
-        LogDebug("DB::Crypto error: transaction error: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
-    } catch (DB::Crypto::Exception::InternalError &e) {
-        LogDebug("DB::Crypto internal error: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
     } catch (const CKM::Exception &e) {
         LogError("CKM::Exception: " << e.GetMessage());
         retCode = CKM_API_ERROR_SERVER_ERROR;
@@ -1359,12 +1327,6 @@ RawBuffer CKMLogic::createKeyAES(
     } catch (std::invalid_argument &e) {
         LogDebug("invalid argument error: " << e.what());
         retCode = CKM_API_ERROR_INPUT_PARAM;
-    } catch (DB::Crypto::Exception::TransactionError &e) {
-        LogDebug("DB::Crypto error: transaction error: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
-    } catch (DB::Crypto::Exception::InternalError &e) {
-        LogDebug("DB::Crypto internal error: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
     } catch (const CKM::Exception &e) {
         LogError("CKM::Exception: " << e.GetMessage());
         retCode = CKM_API_ERROR_SERVER_ERROR;
@@ -1513,9 +1475,6 @@ RawBuffer CKMLogic::getCertificateChain(
                                             chainRawVector);
     } catch (const Exc::Exception &e) {
         retCode = e.error();
-    } catch (const DB::Crypto::Exception::Base &e) {
-        LogError("DB::Crypto failed with message: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
     } catch (const std::exception& e) {
         LogError("STD exception " << e.what());
         retCode = CKM_API_ERROR_SERVER_ERROR;
@@ -1548,9 +1507,6 @@ RawBuffer CKMLogic::getCertificateChain(
                                             trustedCertificates,
                                             useTrustedSystemCertificates,
                                             chainRawVector);
-    } catch (const DB::Crypto::Exception::Base &e) {
-        LogError("DB::Crypto failed with message: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
     } catch (const Exc::Exception &e) {
         retCode = e.error();
     } catch (const std::exception& e) {
@@ -1587,14 +1543,14 @@ RawBuffer CKMLogic::createSignature(
         if(retCode == CKM_API_SUCCESS) {
             signature = obj->sign(cryptoAlg, message);
         }
-    } catch (const DB::Crypto::Exception::Base &e) {
-        LogError("DB::Crypto failed with message: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
     } catch (const Exc::Exception &e) {
         retCode = e.error();
     } catch (const CKM::Exception &e) {
         LogError("Unknown CKM::Exception: " << e.GetMessage());
         retCode = CKM_API_ERROR_SERVER_ERROR;
+    } catch (const std::exception &e) {
+        LogError("STD exception " << e.what());
+        retCode = CKM_API_ERROR_SERVER_ERROR;
     }
 
     auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_SIGNATURE),
@@ -1633,9 +1589,6 @@ RawBuffer CKMLogic::verifySignature(
         }
     } catch (const Exc::Exception &e) {
         retCode = e.error();
-    } catch (const DB::Crypto::Exception::Base &e) {
-        LogError("DB::Crypto failed with message: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
     } catch (const CKM::Exception &e) {
         LogError("Unknown CKM::Exception: " << e.GetMessage());
         retCode = CKM_API_ERROR_SERVER_ERROR;
@@ -1710,12 +1663,12 @@ RawBuffer CKMLogic::setPermission(
         const PermissionMask permissionMask)
 {
     int retCode;
-    Try {
+    try {
         retCode = setPermissionHelper(cred, name, label, accessorLabel, permissionMask);
     } catch (const Exc::Exception &e) {
         retCode = e.error();
-    } Catch (CKM::Exception) {
-        LogError("Error in set row!");
+    } catch (const CKM::Exception &e) {
+        LogError("Error: " << e.GetMessage());
         retCode = CKM_API_ERROR_DB_ERROR;
     }
 
index 99c0155..572b2a0 100644 (file)
@@ -25,6 +25,7 @@
 #include <dpl/db/sql_connection.h>
 #include <dpl/log/log.h>
 #include <ckm/ckm-error.h>
+#include <exception.h>
 
 #pragma GCC diagnostic push
 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
@@ -157,17 +158,13 @@ namespace DB {
             initDatabase();
             m_connection->ExecCommand("VACUUM;");
         } Catch(SqlConnection::Exception::ConnectionBroken) {
-            LogError("Couldn't connect to database: " << path);
-            ReThrow(Crypto::Exception::InternalError);
+            ThrowErr(Exc::DatabaseFailed, "Couldn't connect to database: ", path, _rethrown_exception.GetMessage());
         } Catch(SqlConnection::Exception::InvalidArguments) {
-            LogError("Couldn't set the key for database");
-            ReThrow(Crypto::Exception::InternalError);
+            ThrowErr(Exc::DatabaseFailed, "Couldn't set the key for database. ", _rethrown_exception.GetMessage());
         } Catch(SqlConnection::Exception::SyntaxError) {
-            LogError("Couldn't initiate the database");
-            ReThrow(Crypto::Exception::InternalError);
+            ThrowErr(Exc::DatabaseFailed, "Couldn't initiate the database. ", _rethrown_exception.GetMessage());
         } Catch(SqlConnection::Exception::InternalError) {
-            LogError("Couldn't create the database");
-            ReThrow(Crypto::Exception::InternalError);
+            ThrowErr(Exc::DatabaseFailed, "Couldn't create the database. ", _rethrown_exception.GetMessage());
         }
     }
 
@@ -314,9 +311,7 @@ namespace DB {
         ScriptOptional script = getScript(SCRIPT_CREATE_SCHEMA);
         if(!script)
         {
-            std::string errmsg = "Can not create the database schema: no initialization script";
-            LogError(errmsg);
-            ThrowMsg(Exception::InternalError, errmsg);
+            ThrowErr(Exc::DatabaseFailed, "Can not create the database schema: no initialization script");
         }
 
         m_connection->ExecCommand((*script).c_str());
@@ -330,9 +325,7 @@ namespace DB {
         ScriptOptional script = getScript(SCRIPT_DROP_ALL_ITEMS);
         if(!script)
         {
-            std::string errmsg = "Can not clear the database: no clearing script";
-            LogError(errmsg);
-            ThrowMsg(Exception::InternalError, errmsg);
+            ThrowErr(Exc::DatabaseFailed, "Can not clear the database: no clearing script");
         }
 
         m_connection->ExecCommand((*script).c_str());
@@ -349,8 +342,7 @@ namespace DB {
         } Catch(SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute insert statement");
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't check if name and label pair is present");
+        ThrowErr(Exc::DatabaseFailed, "Couldn't check if name and label pair is present");
     }
 
     void Crypto::saveRows(const Name &name, const Label &owner, const RowVector &rows)
@@ -373,8 +365,7 @@ namespace DB {
         } Catch(SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute insert statement: " << _rethrown_exception.GetMessage());
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't save Row");
+        ThrowErr(Exc::DatabaseFailed, "Couldn't save Row");
     }
 
     void Crypto::saveRow(const Row &row) {
@@ -395,8 +386,7 @@ namespace DB {
         } Catch(SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute insert statement");
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't save Row");
+        ThrowErr(Exc::DatabaseFailed, "Couldn't save Row");
     }
 
     void Crypto::updateRow(const Row &row) {
@@ -410,8 +400,7 @@ namespace DB {
         } Catch(SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute update statement");
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't update Row");
+        ThrowErr(Exc::DatabaseFailed, "Couldn't update Row");
     }
 
     bool Crypto::deleteRow(
@@ -432,8 +421,8 @@ namespace DB {
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute delete statement");
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't delete Row for name " << name << " using ownerLabel " << ownerLabel);
+        ThrowErr(Exc::DatabaseFailed,
+                "Couldn't delete Row for name ", name, " using ownerLabel ", ownerLabel);
     }
 
     Row Crypto::getRow(
@@ -512,11 +501,11 @@ namespace DB {
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute select statement");
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't get row of type <" <<
-                static_cast<int>(typeRangeStart) << "," <<
-                static_cast<int>(typeRangeStop)  << ">" <<
-                " name " << name << " with owner label " << ownerLabel);
+        ThrowErr(Exc::DatabaseFailed,
+                "Couldn't get row of type <",
+                static_cast<int>(typeRangeStart), ",",
+                static_cast<int>(typeRangeStop), ">",
+                " name ", name, " with owner label ", ownerLabel);
     }
 
     void Crypto::getRows(
@@ -558,11 +547,11 @@ namespace DB {
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute select statement");
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't get row of type <" <<
-                static_cast<int>(typeRangeStart) << "," <<
-                static_cast<int>(typeRangeStop)  << ">" <<
-                " name " << name << " with owner label " << ownerLabel);
+        ThrowErr(Exc::DatabaseFailed,
+                "Couldn't get row of type <",
+                static_cast<int>(typeRangeStart), ",",
+                static_cast<int>(typeRangeStop), ">",
+                " name ", name, " with owner label ", ownerLabel);
     }
 
     void Crypto::listNames(
@@ -601,11 +590,11 @@ namespace DB {
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute select statement");
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't list names of type <" <<
-                static_cast<int>(typeRangeStart) << "," <<
-                static_cast<int>(typeRangeStop)  << ">" <<
-                " accessible to client label " << smackLabel);
+        ThrowErr(Exc::DatabaseFailed,
+                "Couldn't list names of type <",
+                static_cast<int>(typeRangeStart), ",",
+                static_cast<int>(typeRangeStop), ">",
+                " accessible to client label ", smackLabel);
     }
 
 
@@ -626,8 +615,7 @@ namespace DB {
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute insert statement");
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't save key for label " << label);
+        ThrowErr(Exc::DatabaseFailed, "Couldn't save key for label ", label);
     }
 
     Crypto::RawBufferOptional Crypto::getKey(const Label& label)
@@ -651,8 +639,7 @@ namespace DB {
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute insert statement");
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't get key for label " << label);
+        ThrowErr(Exc::DatabaseFailed, "Couldn't get key for label ", label);
     }
 
     void Crypto::deleteKey(const Label& label) {
@@ -674,8 +661,7 @@ namespace DB {
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute insert statement");
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't delete key for label " << label);
+        ThrowErr(Exc::DatabaseFailed, "Couldn't delete key for label ", label);
     }
 
     void Crypto::setPermission(
@@ -693,8 +679,7 @@ namespace DB {
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute set statement");
         }
-        ThrowMsg(Crypto::Exception::InternalError,
-                "Couldn't set permissions for name " << name );
+        ThrowErr(Exc::DatabaseFailed, "Couldn't set permissions for name ", name);
     }
 
 
index dae0031..fff626e 100644 (file)
@@ -29,6 +29,7 @@
 #include <dpl/db/sql_connection.h>
 
 #include <ckm/ckm-type.h>
+#include <exception.h>
 #include <db-row.h>
 #include <permission.h>
 #include <protocols.h>
@@ -42,14 +43,6 @@ namespace DB {
          public:
             typedef boost::optional<Row> RowOptional;
             typedef boost::optional<RawBuffer> RawBufferOptional;
-            class Exception
-            {
-            public:
-                DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
-                DECLARE_EXCEPTION_TYPE(Base, InternalError)
-                DECLARE_EXCEPTION_TYPE(Base, TransactionError)
-                DECLARE_EXCEPTION_TYPE(Base, InvalidArgs)
-            };
             Crypto() :
                 m_connection(NULL),
                 m_inUserTransaction(false)
@@ -153,11 +146,9 @@ namespace DB {
                             m_db->m_inUserTransaction = true;
                             m_inTransaction = true;
                         } Catch (SqlConnection::Exception::InternalError) {
-                            LogError("sqlite got into infinite busy state");
-                            ReThrow(Crypto::Exception::TransactionError);
+                            ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
                         } Catch (SqlConnection::Exception::Base) {
-                            LogError("Couldn't begin transaction");
-                            ReThrow(Crypto::Exception::TransactionError);
+                            ThrowErr(Exc::TransactionFailed, "Couldn't begin transaction");
                         }
                     }
                 }
@@ -168,11 +159,9 @@ namespace DB {
                             m_db->m_inUserTransaction = false;
                             m_inTransaction = false;
                         } Catch (SqlConnection::Exception::InternalError) {
-                            LogError("sqlite got into infinite busy state");
-                            ReThrow(Crypto::Exception::TransactionError);
+                            ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
                         } Catch (SqlConnection::Exception::Base) {
-                            LogError("Couldn't commit transaction");
-                            ReThrow(Crypto::Exception::TransactionError);
+                            ThrowErr(Exc::TransactionFailed, "Couldn't commit transaction");
                         }
                     }
                 }
@@ -183,11 +172,9 @@ namespace DB {
                             m_db->m_inUserTransaction = false;
                             m_inTransaction = false;
                         } Catch (SqlConnection::Exception::InternalError) {
-                            LogError("sqlite got into infinite busy state");
-                            ReThrow(Crypto::Exception::TransactionError);
+                            ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
                         } Catch (SqlConnection::Exception::Base) {
-                            LogError("Couldn't rollback transaction");
-                            ReThrow(Crypto::Exception::TransactionError);
+                            ThrowErr(Exc::TransactionFailed, "Couldn't rollback transaction");
                         }
                     }
                 }
@@ -198,8 +185,7 @@ namespace DB {
                             m_db->m_connection->RollbackTransaction();
                         }
                     } Catch (SqlConnection::Exception::InternalError) {
-                        LogError("sqlite got into infinite busy state");
-                        ReThrow(Crypto::Exception::TransactionError);
+                        ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
                     } Catch (SqlConnection::Exception::Base) {
                         LogError("Transaction rollback failed!");
                     }