Improve error handling

This commit is contained in:
eelke 2022-08-17 18:18:10 +02:00
parent 457b09f15c
commit 80272e81c3
5 changed files with 68 additions and 84 deletions

View file

@ -166,7 +166,8 @@ QVariant TablesTableModel::data(const QModelIndex &index, int role) const
return getData(index);
else if (role == CustomDataTypeRole)
return getType(index.column());
else if (role == CustomDataMeaningRole) {
else if (role == CustomDataMeaningRole)
{
switch (index.column()) {
case TotalSizeCol:
case TableSizeCol:
@ -191,9 +192,7 @@ void TablesTableModel::StartLoadTableSizes(std::map<Oid, int> oidIndex)
.then(qApp, [p, oidIndex] (TableSizes sizes)
{
if (p)
{
p.data()->PopulateSizes(std::move(oidIndex), std::move(sizes));
}
});
}

View file

@ -16,6 +16,7 @@
#include "json/json.h"
#include "OpenDatabase.h"
#include "catalog/PgDatabaseCatalog.h"
#include "Pgsql_PgException.h"
#include "QueryParamListController.h"
#include "util.h"
#include "UserConfiguration.h"
@ -155,16 +156,23 @@ void QueryTool::execute()
auto cb = [this](Expected<std::shared_ptr<Pgsql::Result>> res, qint64 elapsedms)
{
if (res.valid()) {
auto && dbresult = res.get();
try
{
auto dbres = res.get();
QMetaObject::invokeMethod(this, "query_ready",
Q_ARG(std::shared_ptr<Pgsql::Result>, dbresult),
Q_ARG(std::shared_ptr<Pgsql::Result>, dbres),
Q_ARG(qint64, elapsedms));
}
else {
/// \todo handle error
catch (Pgsql::PgException &ex)
{
//QMessageBox::critical(this, "pglab", QString::fromStdString(ex.what()));
QMetaObject::invokeMethod(this, [this, ex] ()
{
QMessageBox::critical(this, "pglab", QString::fromStdString(ex.what()));
});
}
};
try {
@ -454,7 +462,13 @@ std::string QueryTool::getCommandUtf8() const
void QueryTool::query_ready(std::shared_ptr<Pgsql::Result> dbres, qint64 elapsedms)
{
if (dbres) {
if (!dbres)
{
m_stopwatch.stop();
addLog("query_ready with NO result");
return;
}
addLog("query_ready with result");
auto st = dbres->resultStatus();
if (st == PGRES_TUPLES_OK) {
@ -488,33 +502,6 @@ void QueryTool::query_ready(std::shared_ptr<Pgsql::Result> dbres, qint64 elapsed
ui->tabWidget->setCurrentWidget(ui->messageTab);
}
else {
// if (st == PGRES_EMPTY_QUERY) {
// statusBar()->showMessage(tr("Empty query."));
// }
// else if (st == PGRES_COPY_OUT) {
// statusBar()->showMessage(tr("COPY OUT."));
// }
// else if (st == PGRES_COPY_IN) {
// statusBar()->showMessage(tr("COPY IN."));
// }
// else if (st == PGRES_BAD_RESPONSE) {
// statusBar()->showMessage(tr("BAD RESPONSE."));
// }
// else if (st == PGRES_NONFATAL_ERROR) {
// statusBar()->showMessage(tr("NON FATAL ERROR."));
// }
// else if (st == PGRES_FATAL_ERROR) {
// statusBar()->showMessage(tr("FATAL ERROR."));
// }
// else if (st == PGRES_COPY_BOTH) {
// statusBar()->showMessage(tr("COPY BOTH shouldn't happen is for replication."));
// }
// else if (st == PGRES_SINGLE_TUPLE) {
// statusBar()->showMessage(tr("SINGLE TUPLE result."));
// }
// else {
// statusBar()->showMessage(tr("No tuples returned, possibly an error..."));
// }
ui->tabWidget->setCurrentWidget(ui->messageTab);
auto details = dbres->diagDetails();
markError(details);
@ -522,11 +509,6 @@ void QueryTool::query_ready(std::shared_ptr<Pgsql::Result> dbres, qint64 elapsed
}
}
}
else {
m_stopwatch.stop();
addLog("query_ready with NO result");
}
}
void QueryTool::markError(const Pgsql::ErrorDetails &details)
{

View file

@ -427,7 +427,7 @@ void ASyncDBConnection::closeConnection()
bool ASyncDBConnection::send(const std::string &command, on_result_callback on_result)
{
if (command.empty())
if (command.empty() || state() == State::NotConnected)
return false;
{
@ -440,6 +440,9 @@ bool ASyncDBConnection::send(const std::string &command, on_result_callback on_r
bool ASyncDBConnection::send(const std::string &command, Pgsql::Params params, on_result_callback on_result)
{
if (command.empty() || state() == State::NotConnected)
return false;
{
std::lock_guard<std::mutex> lg(m_threadData->m_commandQueue.m_mutex);
m_threadData->m_commandQueue.m_queue.emplace(command, std::move(params), on_result);

View file

@ -246,7 +246,7 @@ void Connection::throwError(PGresult *result) const
{
auto state = PQresultStatus(result);
if (state == PGRES_BAD_RESPONSE)
; // communication problem
throw PgException("Communication issue");
else if (state == PGRES_FATAL_ERROR)
{
auto details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);