Error messages: Properly catch and log DB IDO errors.

Refs #6070
This commit is contained in:
Michael Friedrich 2014-06-05 18:26:38 +02:00
parent 09ad04b09a
commit 93992373be
2 changed files with 65 additions and 12 deletions

View File

@ -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))
BOOST_THROW_EXCEPTION(std::bad_alloc());
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();
}

View File

@ -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)