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

@ -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();
QMetaObject::invokeMethod(this, "query_ready",
Q_ARG(std::shared_ptr<Pgsql::Result>, dbresult),
Q_ARG(qint64, elapsedms));
}
else {
/// \todo handle error
try
{
auto dbres = res.get();
QMetaObject::invokeMethod(this, "query_ready",
Q_ARG(std::shared_ptr<Pgsql::Result>, dbres),
Q_ARG(qint64, elapsedms));
}
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,78 +462,52 @@ std::string QueryTool::getCommandUtf8() const
void QueryTool::query_ready(std::shared_ptr<Pgsql::Result> dbres, qint64 elapsedms)
{
if (dbres) {
addLog("query_ready with result");
auto st = dbres->resultStatus();
if (st == PGRES_TUPLES_OK) {
//int n_rows = dbres->getRows();
//QString rowcount_str = QString("rows: %1").arg(dbres->getRows());
if (!dbres)
{
m_stopwatch.stop();
addLog("query_ready with NO result");
return;
}
auto result_model = std::make_shared<QueryResultModel>(nullptr , dbres,
m_catalog);
TuplesResultWidget *trw = new TuplesResultWidget;
trw->setResult(result_model, elapsedms);
resultList.push_back(trw);
ui->tabWidget->addTab(trw, "Data");
if (resultList.size() == 1)
ui->tabWidget->setCurrentWidget(trw);
addLog("query_ready with result");
auto st = dbres->resultStatus();
if (st == PGRES_TUPLES_OK) {
//int n_rows = dbres->getRows();
//QString rowcount_str = QString("rows: %1").arg(dbres->getRows());
}
else {
if (st == PGRES_COMMAND_OK) {
int tuples_affected = dbres->tuplesAffected();
QString msg;
if (tuples_affected >= 0)
msg = tr("Query returned succesfully: %1 rows affected, execution time %2")
.arg(QString::number(tuples_affected))
.arg(msfloatToHumanReadableString(elapsedms));
else
msg = tr("Query returned succesfully, execution time %1")
.arg(msfloatToHumanReadableString(elapsedms));
auto result_model = std::make_shared<QueryResultModel>(nullptr , dbres,
m_catalog);
TuplesResultWidget *trw = new TuplesResultWidget;
trw->setResult(result_model, elapsedms);
resultList.push_back(trw);
ui->tabWidget->addTab(trw, "Data");
if (resultList.size() == 1)
ui->tabWidget->setCurrentWidget(trw);
ui->messagesEdit->append(msg);
}
else {
if (st == PGRES_COMMAND_OK) {
int tuples_affected = dbres->tuplesAffected();
QString msg;
if (tuples_affected >= 0)
msg = tr("Query returned succesfully: %1 rows affected, execution time %2")
.arg(QString::number(tuples_affected))
.arg(msfloatToHumanReadableString(elapsedms));
else
msg = tr("Query returned succesfully, execution time %1")
.arg(msfloatToHumanReadableString(elapsedms));
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);
receiveNotice(details);
}
}
}
else {
m_stopwatch.stop();
addLog("query_ready with NO result");
}
ui->messagesEdit->append(msg);
ui->tabWidget->setCurrentWidget(ui->messageTab);
}
else {
ui->tabWidget->setCurrentWidget(ui->messageTab);
auto details = dbres->diagDetails();
markError(details);
receiveNotice(details);
}
}
}
void QueryTool::markError(const Pgsql::ErrorDetails &details)