[icinga-checkins] icinga.org: icinga2/feature/error-msg-6070: Error messages: Properly catch and log DB IDO errors.

git at icinga.org git at icinga.org
Thu Jun 5 18:27:12 CEST 2014


Module: icinga2
Branch: feature/error-msg-6070
Commit: 93992373be548428865bbfcafc1debfe8012f42d
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=93992373be548428865bbfcafc1debfe8012f42d

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Thu Jun  5 18:26:38 2014 +0200

Error messages: Properly catch and log DB IDO errors.

Refs #6070

---

 components/db_ido_mysql/idomysqlconnection.cpp |   44 ++++++++++++++++++++----
 components/db_ido_pgsql/idopgsqlconnection.cpp |   31 ++++++++++++++---
 2 files changed, 64 insertions(+), 11 deletions(-)

diff --git a/components/db_ido_mysql/idomysqlconnection.cpp b/components/db_ido_mysql/idomysqlconnection.cpp
index 7bacf3f..4f5671d 100644
--- a/components/db_ido_mysql/idomysqlconnection.cpp
+++ b/components/db_ido_mysql/idomysqlconnection.cpp
@@ -94,7 +94,9 @@ void IdoMysqlConnection::Pause(void)
 
 void IdoMysqlConnection::ExceptionHandler(boost::exception_ptr exp)
 {
-	Log(LogWarning, "IdoMysqlConnection", "Exception during database operation: " + DiagnosticInformation(exp));
+	Log(LogCritical, "IdoMysqlConnection", "Exception during database operation: Verify that your database is operational!");
+
+	Log(LogDebug, "IdoMysqlConnection", "Exception during database operation: " + DiagnosticInformation(exp));
 
 	boost::mutex::scoped_lock lock(m_ConnectionMutex);
 
@@ -186,11 +188,22 @@ void IdoMysqlConnection::Reconnect(void)
 		passwd = (!ipasswd.IsEmpty()) ? ipasswd.CStr() : NULL;
 		db = (!idb.IsEmpty()) ? idb.CStr() : NULL;
 
-		if (!mysql_init(&m_Connection))
+		if (!mysql_init(&m_Connection)) {
+			std::ostringstream msgbuf;
+			msgbuf << "mysql_init() failed: \"" << mysql_error(&m_Connection) << "\"";
+			Log(LogCritical, "IdoMysqlConnection", msgbuf.str());
+
 			BOOST_THROW_EXCEPTION(std::bad_alloc());
+		}
+
+		if (!mysql_real_connect(&m_Connection, host, user, passwd, db, port, NULL, CLIENT_FOUND_ROWS)) {
+			std::ostringstream msgbuf;
+			msgbuf << "Connection to database '" << db << "' with user '" << user << "' on '" << host << ":" << port
+			    << "' failed: \"" << mysql_error(&m_Connection) << "\"";
+			Log(LogCritical, "IdoMysqlConnection", msgbuf.str());
 
-		if (!mysql_real_connect(&m_Connection, host, user, passwd, db, port, NULL, CLIENT_FOUND_ROWS))
 			BOOST_THROW_EXCEPTION(std::runtime_error(mysql_error(&m_Connection)));
+		}
 
 		m_Connected = true;
 
@@ -199,16 +212,21 @@ void IdoMysqlConnection::Reconnect(void)
 
 		Dictionary::Ptr version_row = FetchRow(result);
 
-		if (!version_row)
+		if (!version_row) {
+			Log(LogCritical, "IdoMysqlConnection", "Schema does not provide any valid version! Verify your schema installation.");
 			BOOST_THROW_EXCEPTION(std::runtime_error("Schema does not provide any valid version! Verify your schema installation."));
+		}
 
 		DiscardRows(result);
 
 		String version = version_row->Get("version");
 
 		if (Utility::CompareVersion(SCHEMA_VERSION, version) < 0) {
+			Log(LogCritical, "IdoMysqlConnection", "Schema version '" + version + "' does not match the required version '" +
+			   SCHEMA_VERSION + "'! Please check the upgrade documentation.");
+
 			BOOST_THROW_EXCEPTION(std::runtime_error("Schema version '" + version + "' does not match the required version '" +
-			   SCHEMA_VERSION + "'! Please check the upgrade documentation."));
+			   SCHEMA_VERSION + "'!"));
 		}
 
 		String instanceName = GetInstanceName();
@@ -286,24 +304,36 @@ IdoMysqlResult IdoMysqlConnection::Query(const String& query)
 
 	Log(LogDebug, "IdoMysqlConnection", "Query: " + query);
 
-	if (mysql_query(&m_Connection, query.CStr()) != 0)
+	if (mysql_query(&m_Connection, query.CStr()) != 0) {
+		std::ostringstream msgbuf;
+		String message = mysql_error(&m_Connection);
+		msgbuf << "Error \"" << message << "\" when executing query \"" << query << "\"";
+		Log(LogCritical, "IdoMysqlConnection", msgbuf.str());
+
 		BOOST_THROW_EXCEPTION(
 		    database_error()
 		        << errinfo_message(mysql_error(&m_Connection))
 			<< errinfo_database_query(query)
 		);
+	}
 
 	m_AffectedRows = mysql_affected_rows(&m_Connection);
 
 	MYSQL_RES *result = mysql_use_result(&m_Connection);
 
 	if (!result) {
-		if (mysql_field_count(&m_Connection) > 0)
+		if (mysql_field_count(&m_Connection) > 0) {
+			std::ostringstream msgbuf;
+			String message = mysql_error(&m_Connection);
+			msgbuf << "Error \"" << message << "\" when executing query \"" << query << "\"";
+			Log(LogCritical, "IdoMysqlConnection", msgbuf.str());
+
 			BOOST_THROW_EXCEPTION(
 			    database_error()
 				<< errinfo_message(mysql_error(&m_Connection))
 				<< errinfo_database_query(query)
 			);
+		}
 
 		return IdoMysqlResult();
 	}
diff --git a/components/db_ido_pgsql/idopgsqlconnection.cpp b/components/db_ido_pgsql/idopgsqlconnection.cpp
index d9fe92a..126bb46 100644
--- a/components/db_ido_pgsql/idopgsqlconnection.cpp
+++ b/components/db_ido_pgsql/idopgsqlconnection.cpp
@@ -96,7 +96,9 @@ void IdoPgsqlConnection::Pause(void)
 
 void IdoPgsqlConnection::ExceptionHandler(boost::exception_ptr exp)
 {
-	Log(LogWarning, "IdoPgsqlConnection", "Exception during database operation: " + DiagnosticInformation(exp));
+	Log(LogWarning, "IdoPgsqlConnection", "Exception during database operation: Verify that your database is operational!");
+
+	Log(LogDebug, "IdoPgsqlConnection", "Exception during database operation: " + DiagnosticInformation(exp));
 
 	boost::mutex::scoped_lock lock(m_ConnectionMutex);
 
@@ -199,6 +201,11 @@ void IdoPgsqlConnection::Reconnect(void)
 			PQfinish(m_Connection);
 			m_Connection = NULL;
 
+			std::ostringstream msgbuf;
+			msgbuf << "Connection to database '" << db << "' with user '" << user << "' on '" << host << ":" << port
+			    << "' failed: \"" << message << "\"";
+			Log(LogCritical, "IdoPgsqlConnection", msgbuf.str());
+
 			BOOST_THROW_EXCEPTION(std::runtime_error(message));
 		}
 
@@ -207,14 +214,19 @@ void IdoPgsqlConnection::Reconnect(void)
 
 		Dictionary::Ptr version_row = FetchRow(result, 0);
 
-		if (!version_row)
+		if (!version_row) {
+			Log(LogCritical, "IdoPgsqlConnection", "Schema does not provide any valid version! Verify your schema installation.");
 			BOOST_THROW_EXCEPTION(std::runtime_error("Schema does not provide any valid version! Verify your schema installation."));
+		}
 
 		String version = version_row->Get("version");
 
 		if (Utility::CompareVersion(SCHEMA_VERSION, version) < 0) {
+			Log(LogCritical, "IdoPgsqlConnection", "Schema version '" + version + "' does not match the required version '" +
+			    SCHEMA_VERSION + "'! Please check the upgrade documentation.");
+
 			BOOST_THROW_EXCEPTION(std::runtime_error("Schema version '" + version + "' does not match the required version '" +
-			   SCHEMA_VERSION + "'! Please check the upgrade documentation."));
+			   SCHEMA_VERSION + "'!"));
 		}
 
 		String instanceName = GetInstanceName();
@@ -292,11 +304,18 @@ IdoPgsqlResult IdoPgsqlConnection::Query(const String& query)
 
 	PGresult *result = PQexec(m_Connection, query.CStr());
 
-	if (!result)
+	if (!result) {
+		String message = PQerrorMessage(m_Connection);
+		std::ostringstream msgbuf;
+		msgbuf << "Error \"" << message << "\" when executing query \"" << query << "\"";
+		Log(LogCritical, "IdoPgsqlConnection", msgbuf.str());
+
 		BOOST_THROW_EXCEPTION(
 		    database_error()
+		        << errinfo_message(message)
 		        << errinfo_database_query(query)
 		);
+	}
 
 	char *rowCount = PQcmdTuples(result);
 	m_AffectedRows = atoi(rowCount);
@@ -308,6 +327,10 @@ IdoPgsqlResult IdoPgsqlConnection::Query(const String& query)
 		String message = PQresultErrorMessage(result);
 		PQclear(result);
 
+		std::ostringstream msgbuf;
+		msgbuf << "Error \"" << message << "\" when executing query \"" << query << "\"";
+		Log(LogCritical, "IdoPgsqlConnection", msgbuf.str());
+
 		BOOST_THROW_EXCEPTION(
 		    database_error()
 		        << errinfo_message(message)



More information about the icinga-checkins mailing list