mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 09:46:02 -04:00
Merge pull request #138 from fekir/enhance_sqlite_exception
Enhance sqlite exception with overloaded constructor and default copy constructor and assignment operator
This commit is contained in:
commit
8d0ef9700a
@ -134,6 +134,7 @@ set(SQLITECPP_TESTS
|
|||||||
tests/Backup_test.cpp
|
tests/Backup_test.cpp
|
||||||
tests/Transaction_test.cpp
|
tests/Transaction_test.cpp
|
||||||
tests/VariadicBind_test.cpp
|
tests/VariadicBind_test.cpp
|
||||||
|
tests/Exception_test.cpp
|
||||||
)
|
)
|
||||||
source_group(tests FILES ${SQLITECPP_TESTS})
|
source_group(tests FILES ${SQLITECPP_TESTS})
|
||||||
|
|
||||||
|
@ -50,6 +50,7 @@ public:
|
|||||||
*
|
*
|
||||||
* @param[in] aErrorMessage The string message describing the SQLite error
|
* @param[in] aErrorMessage The string message describing the SQLite error
|
||||||
*/
|
*/
|
||||||
|
explicit Exception(const char* aErrorMessage);
|
||||||
explicit Exception(const std::string& aErrorMessage);
|
explicit Exception(const std::string& aErrorMessage);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,6 +59,7 @@ public:
|
|||||||
* @param[in] aErrorMessage The string message describing the SQLite error
|
* @param[in] aErrorMessage The string message describing the SQLite error
|
||||||
* @param[in] ret Return value from function call that failed.
|
* @param[in] ret Return value from function call that failed.
|
||||||
*/
|
*/
|
||||||
|
Exception(const char* aErrorMessage, int ret);
|
||||||
Exception(const std::string& aErrorMessage, int ret);
|
Exception(const std::string& aErrorMessage, int ret);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -91,8 +93,8 @@ public:
|
|||||||
const char* getErrorStr() const noexcept; // nothrow
|
const char* getErrorStr() const noexcept; // nothrow
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const int mErrcode; ///< Error code value
|
int mErrcode; ///< Error code value
|
||||||
const int mExtendedErrcode; ///< Detailed error code if any
|
int mExtendedErrcode; ///< Detailed error code if any
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,6 +16,12 @@
|
|||||||
namespace SQLite
|
namespace SQLite
|
||||||
{
|
{
|
||||||
|
|
||||||
|
Exception::Exception(const char* aErrorMessage) :
|
||||||
|
std::runtime_error(aErrorMessage),
|
||||||
|
mErrcode(-1), // 0 would be SQLITE_OK, which doesn't make sense
|
||||||
|
mExtendedErrcode(-1)
|
||||||
|
{
|
||||||
|
}
|
||||||
Exception::Exception(const std::string& aErrorMessage) :
|
Exception::Exception(const std::string& aErrorMessage) :
|
||||||
std::runtime_error(aErrorMessage),
|
std::runtime_error(aErrorMessage),
|
||||||
mErrcode(-1), // 0 would be SQLITE_OK, which doesn't make sense
|
mErrcode(-1), // 0 would be SQLITE_OK, which doesn't make sense
|
||||||
@ -23,6 +29,13 @@ Exception::Exception(const std::string& aErrorMessage) :
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Exception::Exception(const char* aErrorMessage, int ret) :
|
||||||
|
std::runtime_error(aErrorMessage),
|
||||||
|
mErrcode(ret),
|
||||||
|
mExtendedErrcode(-1)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
Exception::Exception(const std::string& aErrorMessage, int ret) :
|
Exception::Exception(const std::string& aErrorMessage, int ret) :
|
||||||
std::runtime_error(aErrorMessage),
|
std::runtime_error(aErrorMessage),
|
||||||
mErrcode(ret),
|
mErrcode(ret),
|
||||||
|
66
tests/Exception_test.cpp
Normal file
66
tests/Exception_test.cpp
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/**
|
||||||
|
* @file Transaction_test.cpp
|
||||||
|
* @ingroup tests
|
||||||
|
* @brief Test of a SQLite Transaction.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2012-2016 Sebastien Rombauts (sebastien.rombauts@gmail.com)
|
||||||
|
*
|
||||||
|
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
|
||||||
|
* or copy at http://opensource.org/licenses/MIT)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <SQLiteCpp/Exception.h>
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
TEST(Exception, copy) {
|
||||||
|
const SQLite::Exception ex1("some error", 2);
|
||||||
|
const SQLite::Exception ex2 = ex1;
|
||||||
|
EXPECT_STREQ(ex1.what(), ex2.what());
|
||||||
|
EXPECT_EQ(ex1.getErrorCode(), ex2.getErrorCode());
|
||||||
|
EXPECT_EQ(ex1.getExtendedErrorCode(), ex2.getExtendedErrorCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
// see http://eel.is/c++draft/exception#2 or http://www.cplusplus.com/reference/exception/exception/operator=/
|
||||||
|
// an assignment operator is expected to be avaiable
|
||||||
|
TEST(Exception, assignment) {
|
||||||
|
const SQLite::Exception ex1("some error", 2);
|
||||||
|
SQLite::Exception ex2("some error2", 3);
|
||||||
|
|
||||||
|
ex2 = ex1;
|
||||||
|
|
||||||
|
EXPECT_STREQ(ex1.what(), ex2.what());
|
||||||
|
EXPECT_EQ(ex1.getErrorCode(), ex2.getErrorCode());
|
||||||
|
EXPECT_EQ(ex1.getExtendedErrorCode(), ex2.getExtendedErrorCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Exception, throw_catch) {
|
||||||
|
const char message[] = "some error";
|
||||||
|
try {
|
||||||
|
throw SQLite::Exception(message);
|
||||||
|
} catch (const std::runtime_error& ex) {
|
||||||
|
EXPECT_STREQ(ex.what(), message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST(Exception, constructor) {
|
||||||
|
const char msg1[] = "error msg";
|
||||||
|
std::string msg2 = msg1;
|
||||||
|
{
|
||||||
|
const SQLite::Exception ex1(msg1);
|
||||||
|
const SQLite::Exception ex2(msg2);
|
||||||
|
EXPECT_STREQ(ex1.what(), ex2.what());
|
||||||
|
EXPECT_EQ(ex1.getErrorCode(), ex2.getErrorCode());
|
||||||
|
EXPECT_EQ(ex1.getExtendedErrorCode(), ex2.getExtendedErrorCode());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const SQLite::Exception ex1(msg1, 1);
|
||||||
|
const SQLite::Exception ex2(msg2, 1);
|
||||||
|
EXPECT_STREQ(ex1.what(), ex2.what());
|
||||||
|
EXPECT_EQ(ex1.getErrorCode(), ex2.getErrorCode());
|
||||||
|
EXPECT_EQ(ex1.getExtendedErrorCode(), ex2.getExtendedErrorCode());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user