Better handling of exceptions thrown from contructors

This commit is contained in:
Bill Somerville 2019-05-30 23:21:41 +01:00
parent e1451202f4
commit df4e00605a
No known key found for this signature in database
GPG Key ID: D864B06D1E81618F
2 changed files with 34 additions and 26 deletions

View File

@ -380,10 +380,12 @@ int main(int argc, char *argv[])
} }
catch (std::exception const& e) catch (std::exception const& e)
{ {
MessageBox::critical_message (nullptr, QApplication::translate ("main", "Fatal error"), e.what ());
std::cerr << "Error: " << e.what () << '\n'; std::cerr << "Error: " << e.what () << '\n';
} }
catch (...) catch (...)
{ {
MessageBox::critical_message (nullptr, QApplication::translate ("main", "Unexpected fatal error"));
std::cerr << "Unexpected fatal error\n"; std::cerr << "Unexpected fatal error\n";
throw; // hoping the runtime might tell us more about the exception throw; // hoping the runtime might tell us more about the exception
} }

View File

@ -22,6 +22,7 @@ public:
impl (Configuration const *); impl (Configuration const *);
QString cabrillo_frequency_string (Radio::Frequency frequency) const; QString cabrillo_frequency_string (Radio::Frequency frequency) const;
void create_table ();
Configuration const * configuration_; Configuration const * configuration_;
QSqlQuery mutable dupe_query_; QSqlQuery mutable dupe_query_;
@ -34,19 +35,25 @@ CabrilloLog::impl::impl (Configuration const * configuration)
{ {
if (!database ().tables ().contains ("cabrillo_log")) if (!database ().tables ().contains ("cabrillo_log"))
{ {
QSqlQuery query; create_table ();
SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
"CREATE TABLE cabrillo_log ("
" id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
" frequency INTEGER NOT NULL,"
" \"when\" DATETIME NOT NULL,"
" call VARCHAR(20) NOT NULL,"
" exchange_sent VARCHAR(32) NOT NULL,"
" exchange_rcvd VARCHAR(32) NOT NULL,"
" band VARCHAR(6) NOT NULL"
")");
} }
setTable ("cabrillo_log");
setEditStrategy (QSqlTableModel::OnFieldChange);
setHeaderData (fieldIndex ("frequency"), Qt::Horizontal, tr ("Freq(kHz)"));
setHeaderData (fieldIndex ("when"), Qt::Horizontal, tr ("Date & Time(UTC)"));
setHeaderData (fieldIndex ("call"), Qt::Horizontal, tr ("Call"));
setHeaderData (fieldIndex ("exchange_sent"), Qt::Horizontal, tr ("Sent"));
setHeaderData (fieldIndex ("exchange_rcvd"), Qt::Horizontal, tr ("Rcvd"));
setHeaderData (fieldIndex ("band"), Qt::Horizontal, tr ("Band"));
// This descending order by time is important, it makes the view
// place the latest row at the top, without this the model/view
// interactions are both sluggish and unhelpful.
setSort (fieldIndex ("when"), Qt::DescendingOrder);
SQL_error_check (*this, &QSqlTableModel::select);
SQL_error_check (dupe_query_, &QSqlQuery::prepare, SQL_error_check (dupe_query_, &QSqlQuery::prepare,
"SELECT " "SELECT "
" COUNT(*) " " COUNT(*) "
@ -67,22 +74,21 @@ CabrilloLog::impl::impl (Configuration const * configuration)
" cabrillo_log " " cabrillo_log "
" ORDER BY " " ORDER BY "
" \"when\""); " \"when\"");
}
setEditStrategy (QSqlTableModel::OnFieldChange); void CabrilloLog::impl::create_table ()
setTable ("cabrillo_log"); {
setHeaderData (fieldIndex ("frequency"), Qt::Horizontal, tr ("Freq(kHz)")); QSqlQuery query;
setHeaderData (fieldIndex ("when"), Qt::Horizontal, tr ("Date & Time(UTC)")); SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
setHeaderData (fieldIndex ("call"), Qt::Horizontal, tr ("Call")); "CREATE TABLE cabrillo_log ("
setHeaderData (fieldIndex ("exchange_sent"), Qt::Horizontal, tr ("Sent")); " id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
setHeaderData (fieldIndex ("exchange_rcvd"), Qt::Horizontal, tr ("Rcvd")); " frequency INTEGER NOT NULL,"
setHeaderData (fieldIndex ("band"), Qt::Horizontal, tr ("Band")); " \"when\" DATETIME NOT NULL,"
" call VARCHAR(20) NOT NULL,"
// This descending order by time is important, it makes the view " exchange_sent VARCHAR(32) NOT NULL,"
// place the latest row at the top, without this the model/view " exchange_rcvd VARCHAR(32) NOT NULL,"
// interactions are both sluggish and unhelpful. " band VARCHAR(6) NOT NULL"
setSort (fieldIndex ("when"), Qt::DescendingOrder); ")");
SQL_error_check (*this, &QSqlTableModel::select);
} }
// frequency here is in kHz // frequency here is in kHz