Skip to content

Commit

Permalink
Improve: report errors with loading the profile (#6735)
Browse files Browse the repository at this point in the history
<!-- Keep the title short & concise so anyone non-technical can
understand it,
     the title appears in PTB changelogs -->
#### Brief overview of PR changes/additions
Report any errors that happen while loading a profile
#### Motivation for adding to Mudlet
So if a profile is corrupted for whatever reason (ie, computer crash) -
folks will have a better experience understanding what's going on and
how can they address it
#### Other info (issues closed, discussion etc)
The one rare case of a `TODO` in the codebase actually being fixed :O
  • Loading branch information
vadi2 committed Apr 27, 2023
1 parent 8e4eade commit 6c31d41
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/TMap.cpp
Expand Up @@ -2577,7 +2577,7 @@ bool TMap::readXmlMapFile(QFile& file, QString* errMsg)
mapClear();

XMLimport reader(pHost);
bool result = reader.importPackage(&file);
auto [success, message] = reader.importPackage(&file);

if (!mpMapper.isNull() && mpMapper->mp2dMap) {
// probably not needed for the download but might be
Expand All @@ -2586,12 +2586,18 @@ bool TMap::readXmlMapFile(QFile& file, QString* errMsg)
// No need to call audit() as XMLimport::importPackage() does it!
// audit() produces the successful ending [ OK ] message...!
mpMapper->updateAreaComboBox();
if (result) {
if (success) {
mpMapper->resetAreaComboBoxToPlayerRoomArea();
} else {
// Failed...
if (errMsg) {
*errMsg = tr("loadMap: failure to import XML map file, further information may be available\n"
"in main console!");
}
}
}

if (!result && errMsg) {
if (!success && errMsg) {
*errMsg = tr("loadMap: failure to import XML map file, further information may be available\n"
"in main console!");
}
Expand All @@ -2606,7 +2612,7 @@ bool TMap::readXmlMapFile(QFile& file, QString* errMsg)
mpMapper->show();
}

return result;
return success;
}

void TMap::slot_setDownloadProgress(qint64 got, qint64 total)
Expand Down
6 changes: 3 additions & 3 deletions src/XMLimport.cpp
Expand Up @@ -60,7 +60,7 @@ XMLimport::XMLimport(Host* pH)
{
}

bool XMLimport::importPackage(QFile* pfile, QString packName, int moduleFlag, QString* pVersionString)
std::pair<bool, QString> XMLimport::importPackage(QFile* pfile, QString packName, int moduleFlag, QString* pVersionString)
{
mPackageName = packName;
setDevice(pfile);
Expand Down Expand Up @@ -169,7 +169,7 @@ bool XMLimport::importPackage(QFile* pfile, QString packName, int moduleFlag, QS
"and this one cannot read it, you need a newer Mudlet!")
.arg(pfile->fileName(), versionString);
mpHost->postMessage(moanMsg);
return false;
return {false, moanMsg};
}

readPackage();
Expand Down Expand Up @@ -227,7 +227,7 @@ bool XMLimport::importPackage(QFile* pfile, QString packName, int moduleFlag, QS
}
}

return !error();
return {!hasError(), errorString()};
}

// returns the type of item and ID of the first (root) element
Expand Down
2 changes: 1 addition & 1 deletion src/XMLimport.h
Expand Up @@ -54,7 +54,7 @@ class XMLimport : public QXmlStreamReader
public:
explicit XMLimport(Host*);
virtual ~XMLimport() {}
bool importPackage(QFile*, QString packageName = QString(), int moduleFlag = 0, QString* pVersionString = nullptr);
std::pair<bool, QString> importPackage(QFile*, QString packageName = QString(), int moduleFlag = 0, QString* pVersionString = nullptr);
std::pair<dlgTriggerEditor::EditorViewType, int> importFromClipboard();

private:
Expand Down
12 changes: 10 additions & 2 deletions src/dlgConnectionProfiles.cpp
Expand Up @@ -1547,11 +1547,19 @@ void dlgConnectionProfiles::loadProfile(bool alsoConnect)
if (entries.isEmpty()) {
firstTimeLoad = true;
} else {
QFile file(qsl("%1%2").arg(folder, profile_history->itemData(profile_history->currentIndex()).toString()));
QString fileName{qsl("%1%2").arg(folder, profile_history->itemData(profile_history->currentIndex()).toString())};
QFile file(fileName);
file.open(QFile::ReadOnly | QFile::Text);
XMLimport importer(pHost);

qDebug() << "[LOADING PROFILE]:" << file.fileName();
importer.importPackage(&file, nullptr); // TODO: Missing false return value handler
if (auto [success, message] = importer.importPackage(&file, nullptr); !success) {
pHost->postMessage(tr("[ ERROR ] - Something went wrong loading your Mudlet profile and it could not be loaded.\n"
"Try loading an older version in 'Connect - Options - Profile history' or double-check that %1 looks correct.", "%1 is the filename").arg(fileName));

qDebug() << "dlgConnectionProfiles::loadProfile: ERROR loading" << fileName << "due to:" << message;
}

pHost->refreshPackageFonts();

// Is this a new profile created through 'copy profile (settings only)'? install default packages into it
Expand Down

0 comments on commit 6c31d41

Please sign in to comment.