improved exception handling v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 01 Jun 2020 00:14:40 +0200
branchv_0
changeset 38 a871779a4e3c
parent 37 3de41719d7eb
child 39 b4af13653313
improved exception handling
src/Connection.cpp
src/DriverManager.cpp
src/SqlException.cpp
--- a/src/Connection.cpp	Sun May 31 21:20:24 2020 +0200
+++ b/src/Connection.cpp	Mon Jun 01 00:14:40 2020 +0200
@@ -34,8 +34,7 @@
 
 Connection::~Connection() {
 	SQLRETURN result = SQLDisconnect(connection);
-	// FIXME: do not throw exception from destructor
-	if (OdbcCommon::isNotSuccessful(result)) throw SqlException(L"Unable to disconnect: " + std::to_wstring(result));
+	// TODO: log warning if disconnection failed?
 	OdbcCommon::freeHandle(SQL_HANDLE_DBC, connection);
 }
 
--- a/src/DriverManager.cpp	Sun May 31 21:20:24 2020 +0200
+++ b/src/DriverManager.cpp	Mon Jun 01 00:14:40 2020 +0200
@@ -68,7 +68,7 @@
 			(SQLCHAR*) passwordBytes.c_str(), SQL_NTS);
 	if (OdbcCommon::isNotSuccessful(result)) {
 		OdbcCommon::freeHandle(SQL_HANDLE_DBC, connection);
-		throw SqlException(L"Unable to connect to " + dataSourceName, result, SQL_HANDLE_ENV, environment);
+		throw SqlException(L"Unable to connect to DSN: " + dataSourceName, result, SQL_HANDLE_ENV, environment);
 	}
 	return new Connection(connection);
 }
@@ -82,7 +82,10 @@
 			(SQLCHAR*) convertor.to_bytes(connectionString).c_str(), SQL_NTS,
 			(SQLCHAR*) completeConnectionString, sizeof (completeConnectionString), &completeConnectionStringLength,
 			SQL_DRIVER_NOPROMPT);
-	if (OdbcCommon::isNotSuccessful(result)) throw SqlException(L"Unable to connect to " + connectionString, result, SQL_HANDLE_ENV, environment);
+	if (OdbcCommon::isNotSuccessful(result)) {
+		OdbcCommon::freeHandle(SQL_HANDLE_DBC, connection);
+		throw SqlException(L"Unable to connect to URL: " + connectionString, result, SQL_HANDLE_ENV, environment);
+	}
 	return new Connection(connection);
 }
 
--- a/src/SqlException.cpp	Sun May 31 21:20:24 2020 +0200
+++ b/src/SqlException.cpp	Mon Jun 01 00:14:40 2020 +0200
@@ -30,11 +30,9 @@
 namespace sql {
 
 SqlException::SqlException(std::wstring message) : message(message) {
-	std::wcerr << L"XXX SqlException: " << message.c_str() << std::endl << std::flush; // FIXME: remove
 }
 
 SqlException::SqlException(std::wstring message, SQLRETURN resultCode, SQLSMALLINT handleType, SQLHANDLE handle) : message(message), resultCode(resultCode) {
-	std::wcerr << L"XXX SqlException: " << message.c_str() << std::endl << std::flush; // FIXME: remove
 	std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings
 	SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1];
 	SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1];