Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: continue to load dictionaries when some dictionaries throw exception. #2180

Merged
merged 8 commits into from
Feb 20, 2025
18 changes: 11 additions & 7 deletions src/dict/loaddictionaries.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include <QMessageBox>
#include <QDir>
#include <QString>

#include <set>

Expand All @@ -57,7 +58,6 @@ LoadDictionaries::LoadDictionaries( Config::Class const & cfg ):
soundDirs( cfg.soundDirs ),
hunspell( cfg.hunspell ),
transliteration( cfg.transliteration ),
exceptionText( "Load did not finish" ), // Will be cleared upon success
maxHeadwordSize( cfg.maxHeadwordSize ),
maxHeadwordToExpand( cfg.maxHeadwordsToExpand )
{
Expand Down Expand Up @@ -94,7 +94,15 @@ void LoadDictionaries::run()
try {
for ( const auto & path : paths ) {
qDebug() << "handle path:" << path.path;
handlePath( path );
try {
handlePath( path );
}
catch ( const std::exception & e ) {
qWarning() << "Error handling path:" << path.path << "-" << e.what();
//hold last exception message.
auto exception = "[" + path.path.toStdString() + "]:" + e.what();
exceptionTexts << QString::fromUtf8( exception );
}
}

// Make soundDirs
Expand Down Expand Up @@ -129,11 +137,9 @@ void LoadDictionaries::run()
dict->setFtsEnable( dictMetaData->fullindex.value() );
}
}

exceptionText.clear();
}
catch ( std::exception & e ) {
exceptionText = e.what();
exceptionTexts << QString::fromUtf8( e.what() );
}
}

Expand Down Expand Up @@ -228,8 +234,6 @@ void loadDictionaries( QWidget * parent,
QMessageBox::critical( parent,
QCoreApplication::translate( "LoadDictionaries", "Error loading dictionaries" ),
QString::fromUtf8( loadDicts.getExceptionText().c_str() ) );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here should show all errors.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not want to change too much code. :-)

Copy link
Collaborator

@shenlebantongying shenlebantongying Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error handling should be religious, and silently ignoring errors is sinful 😅.

I think this only need to append the original exceptionText to a QStringList, then call QStringList::join('\n') here.

BTW, I think there should be a systematical way to report dict errors and warnings back to users.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be left from another PR.


return;
}

dictionaries = loadDicts.getDictionaries();
Expand Down
5 changes: 3 additions & 2 deletions src/dict/loaddictionaries.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <QThread>
#include <QNetworkAccessManager>
#include <QStringList>

/// Use loadDictionaries() function below -- this is a helper thread class
class LoadDictionaries: public QThread, public Dictionary::Initializing
Expand All @@ -21,7 +22,7 @@ class LoadDictionaries: public QThread, public Dictionary::Initializing
Config::Hunspell const & hunspell;
Config::Transliteration const & transliteration;
std::vector< sptr< Dictionary::Class > > dictionaries;
std::string exceptionText;
QStringList exceptionTexts;
unsigned int maxHeadwordSize;
unsigned int maxHeadwordToExpand;

Expand All @@ -39,7 +40,7 @@ public:
/// Empty string means to exception occurred
std::string const & getExceptionText() const
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns a ref to temporal object.

Should be std::string getExceptionText() instead.

https://github.com/xiaoyifang/goldendict-ng/actions/runs/13459304050/job/37610490793#step:6:429

{
return exceptionText;
return exceptionTexts.join( "\n" ).toStdString();
}


Expand Down
Loading