Improved error reporting

This commit is contained in:
eelke 2022-08-14 08:04:21 +02:00
parent 6cf7b52453
commit 457b09f15c
12 changed files with 110 additions and 94 deletions

View file

@ -79,10 +79,10 @@ private:
bool makeConnection();
void communicate();
void doStateCallback(ASyncDBConnection::State state);
void doStateCallback(ASyncDBConnection::StateData state);
/// Wait's for a command to come in and send's it to the server
void waitForAndSendCommand();
void doNewCommand();
void doNewCommand();
void waitForResult();
@ -106,33 +106,46 @@ void ASyncDBConnectionThread::run()
SCOPE_EXIT {
m_state = ASyncDBConnection::State::NotConnected;
m_terminated = true;
m_connection.close();
// doStateCallback(ASyncDBConnection::State::NotConnected);
};
while (!terminateRequested) {
// make or recover connection
if (makeConnection()) {
m_connection.setNoticeReceiver(
[this](const PGresult *result) { processNotice(result); });
m_canceller = m_connection.getCancel();
try {
while (!terminateRequested) {
// make or recover connection
if (makeConnection()) {
m_connection.setNoticeReceiver(
[this](const PGresult *result) { processNotice(result); });
m_canceller = m_connection.getCancel();
// send commands and receive results
communicate();
}
else {
// It is not possible to determine the source of the problem.
// Accept for PQconnectionNeedsPassword
// send commands and receive results
communicate();
}
else {
// It is not possible to determine the source of the problem.
// Accept for PQconnectionNeedsPassword
// Pass problem to main thread and stop this thread
// Pass problem to main thread and stop this thread
// Main thread needs to know it has to restart connecting if it want's to.
// TODO: add status functions to help main thread so it doesn't have to remember
// everything reported through callbacks.
// Main thread needs to know it has to restart connecting if it want's to.
// TODO: add status functions to help main thread so it doesn't have to remember
// everything reported through callbacks.
break;
}
}
// close connection
break;
}
}
doStateCallback({ ASyncDBConnection::State::NotConnected, QString("terminating") });
}
catch (const std::exception &ex)
{
doStateCallback({ ASyncDBConnection::State::NotConnected, QString::fromUtf8(ex.what()) });
}
catch (...)
{
doStateCallback({ ASyncDBConnection::State::NotConnected, QString::fromUtf8("Unknown error") });
}
}
@ -146,19 +159,7 @@ bool ASyncDBConnectionThread::makeConnection()
using namespace std::literals::chrono_literals;
// start connecting
// auto keywords = m_config.getKeywords();
// auto values = m_config.getValues();
QString conn_string = m_config.connectionString();
#if false
try {
m_connection.connect(conn_string); //keywords, values, 0);
doStateCallback(ASyncDBConnection::State::Connected);
return true;
}
catch (Pgsql::PgConnectionError &) {
}
return false;
#else
while (!terminateRequested) {
bool ok = m_connection.connectStart(conn_string); //keywords, values);
@ -190,20 +191,21 @@ bool ASyncDBConnectionThread::makeConnection()
if (res == wait_result_socket_event) {
auto poll_state = m_connection.connectPoll();
if (poll_state == PGRES_POLLING_OK) {
qDebug() << "ASyncDBConnection Connected";
// if connected return true
doStateCallback(ASyncDBConnection::State::Connected);
doStateCallback({ ASyncDBConnection::State::Connected, "Success" });
return true;
}
else if (poll_state == PGRES_POLLING_FAILED) {
doStateCallback(ASyncDBConnection::State::NotConnected);
doStateCallback({ ASyncDBConnection::State::NotConnected, "Failed to connect" });
return false;
}
else if (poll_state == PGRES_POLLING_READING) {
doStateCallback(ASyncDBConnection::State::Connecting);
doStateCallback({ ASyncDBConnection::State::Connecting, "Negotiating" });
fd = FD_READ;
}
else if (poll_state == PGRES_POLLING_WRITING) {
doStateCallback(ASyncDBConnection::State::Connecting);
doStateCallback({ ASyncDBConnection::State::Connecting, "Negotiating" });
fd = FD_WRITE;
}
}
@ -216,7 +218,6 @@ bool ASyncDBConnectionThread::makeConnection()
}
}
return false;
#endif
}
void ASyncDBConnectionThread::communicate()
@ -252,13 +253,9 @@ void ASyncDBConnectionThread::stop()
m_stopEvent.set();
}
void ASyncDBConnectionThread::doStateCallback(ASyncDBConnection::State state)
void ASyncDBConnectionThread::doStateCallback(ASyncDBConnection::StateData state)
{
m_state = state;
// std::lock_guard<std::mutex> lg(m_stateCallback.m_mutex);
// if (m_stateCallback.m_func) {
// m_stateCallback.m_func(state);
// }
m_state = state.State;
Q_EMIT asyncConnObject->onStateChanged(state);
}
@ -289,21 +286,13 @@ void ASyncDBConnectionThread::doNewCommand()
const Command &command = m_commandQueue.m_queue.front();
bool query_send = false;
if (command.params.empty())
query_send = m_connection.sendQuery(command.command.c_str());
m_connection.sendQuery(command.command.c_str());
else
query_send = m_connection.sendQueryParams(command.command.c_str(), command.params);
m_connection.sendQueryParams(command.command.c_str(), command.params);
if (query_send) {
m_timer.start();
doStateCallback(ASyncDBConnection::State::QuerySend);
}
else {
std::string error = m_connection.getErrorMessage();
qDebug() << "Error sending query " << QString::fromStdString(error);
// todo: need to report the error
}
m_timer.start();
doStateCallback(ASyncDBConnection::State::QuerySend);
}
}
bool ASyncDBConnectionThread::consumeResultInput()
@ -332,7 +321,7 @@ bool ASyncDBConnectionThread::consumeResultInput()
// error during consume
auto error_msg = m_connection.getErrorMessage();
qDebug() << "error while communicating with server " << QString::fromStdString(error_msg);
doStateCallback(ASyncDBConnection::State::NotConnected);
doStateCallback({ASyncDBConnection::State::NotConnected, QString::fromStdString(error_msg)});
finished = true;
stop();
}