diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-02 21:21:57 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-02 21:21:57 +0000 |
commit | 0768666d941af819c8fc20fc872609d4107c7bd2 (patch) | |
tree | 50b46946e1eb2a52189670661054744e3c5664fc /libpqpp | |
parent | Tidy up and alias glibmm and libxml++ refs (diff) | |
download | libdbpp-postgresql-0768666d941af819c8fc20fc872609d4107c7bd2.tar.bz2 libdbpp-postgresql-0768666d941af819c8fc20fc872609d4107c7bd2.tar.xz libdbpp-postgresql-0768666d941af819c8fc20fc872609d4107c7bd2.zip |
Propergate connection error details into DB::Error and add test
Diffstat (limited to 'libpqpp')
-rw-r--r-- | libpqpp/connection.cpp | 6 | ||||
-rw-r--r-- | libpqpp/error.cpp | 5 | ||||
-rw-r--r-- | libpqpp/error.h | 8 | ||||
-rw-r--r-- | libpqpp/unittests/testpq.cpp | 6 |
4 files changed, 20 insertions, 5 deletions
diff --git a/libpqpp/connection.cpp b/libpqpp/connection.cpp index 98f9054..51aac63 100644 --- a/libpqpp/connection.cpp +++ b/libpqpp/connection.cpp @@ -18,7 +18,7 @@ PQ::Connection::Connection(const std::string & info) : rolledback(false) { if (PQstatus(conn) != CONNECTION_OK) { - throw ConnectionError(); + throw ConnectionError(conn); } PQsetNoticeProcessor(conn, noNoticeProcessor, NULL); } @@ -100,11 +100,11 @@ PQ::Connection::ping() const { if (PQstatus(conn) != CONNECTION_OK) { if (inTx()) { - throw ConnectionError(); + throw ConnectionError(conn); } PQreset(conn); if (PQstatus(conn) != CONNECTION_OK) { - throw ConnectionError(); + throw ConnectionError(conn); } } } diff --git a/libpqpp/error.cpp b/libpqpp/error.cpp index 0886238..8664b70 100644 --- a/libpqpp/error.cpp +++ b/libpqpp/error.cpp @@ -26,3 +26,8 @@ PQ::Error::what() const throw() return msg ? msg : "No message"; } +PQ::ConnectionError::ConnectionError(const PGconn * conn) : + PQ::Error(PQerrorMessage(conn)) +{ +} + diff --git a/libpqpp/error.h b/libpqpp/error.h index 2acbce3..8e7c4bc 100644 --- a/libpqpp/error.h +++ b/libpqpp/error.h @@ -2,9 +2,11 @@ #define PQ_ERROR_H #include <error.h> +#include <libpq-fe.h> +#include <visibility.h> namespace PQ { - class Error : public DB::Error { + class DLL_PUBLIC Error : public DB::Error { public: Error(); Error(const Error &); @@ -16,7 +18,9 @@ namespace PQ { private: char * msg; }; - class ConnectionError : public Error, public virtual DB::ConnectionError { + class DLL_PUBLIC ConnectionError : public Error, public virtual DB::ConnectionError { + public: + ConnectionError(const PGconn *); }; } diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp index cf366cb..5dc4b2d 100644 --- a/libpqpp/unittests/testpq.cpp +++ b/libpqpp/unittests/testpq.cpp @@ -9,6 +9,7 @@ #include <testCore.h> #include <fstream> #include <boost/date_time/posix_time/posix_time.hpp> +#include "../error.h" class StandardMockDatabase : public PQ::Mock { public: @@ -169,3 +170,8 @@ BOOST_AUTO_TEST_CASE( insertId ) BOOST_AUTO_TEST_SUITE_END(); +BOOST_AUTO_TEST_CASE( connfail ) +{ + BOOST_REQUIRE_THROW(DB::ConnectionFactory::createNew("postgresql", "host=localhost user=no"), PQ::ConnectionError); +} + |