Improved error handling when opening files

This commit is contained in:
Jan Käberich 2021-10-12 20:52:11 +02:00
parent 63cb5e4f67
commit e4106fe3a2
3 changed files with 18 additions and 3 deletions

View File

@ -79,7 +79,11 @@ Calkit Calkit::fromFile(QString filename)
}
json j;
file >> j;
try {
file >> j;
} catch (exception &e) {
throw runtime_error("JSON parsing error: " + string(e.what()));
}
if(j.contains("SOLT")) {
qDebug() << "JSON format detected";
// calkit file uses json format, parse

View File

@ -7,6 +7,7 @@
#include <fstream>
#include <touchstone.h>
#include <QtGlobal>
#include <QMessageBox>
using namespace std;
@ -119,7 +120,12 @@ CalkitDialog::CalkitDialog(Calkit &c, QWidget *parent) :
connect(ui->buttonBox->button(QDialogButtonBox::Open), &QPushButton::clicked, [=](){
auto filename = QFileDialog::getOpenFileName(this, "Open calibration kit coefficients", "", "Calibration kit files (*.calkit)", nullptr, QFileDialog::DontUseNativeDialog);
if(filename.length() > 0) {
ownKit = Calkit::fromFile(filename);
try {
ownKit = Calkit::fromFile(filename);
} catch (runtime_error e) {
QMessageBox::warning(nullptr, "Error", "The calibration kit file could not be parsed (" + QString(e.what()) + ")");
qWarning() << "Parsing of calibration kit failed while opening calibration file: " << e.what();
}
updateEntries();
}
});

View File

@ -178,7 +178,12 @@ AppWindow::AppWindow(QWidget *parent)
return;
}
nlohmann::json j;
file >> j;
try {
file >> j;
} catch (exception &e) {
QMessageBox::warning(nullptr, "Error", "Failed to parse the setup file (" + QString(e.what()) + ")");
qWarning() << "Parsing of setup file failed: " << e.what();
}
file.close();
LoadSetup(j);
});