Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/core/BackgroundExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ void BackgroundExecutor::Dispatcher::customEvent(QEvent* event) {
}
} catch (const std::bad_alloc&) {
OutOfMemoryHandler::instance().handleOutOfMemorySituation();
} catch (const std::exception& e) {
// An exception escaping the executor thread would terminate the application.
qWarning("Background task failed: %s", e.what());
} catch (...) {
qWarning("Background task failed with an unknown exception.");
}
}

Expand Down
20 changes: 16 additions & 4 deletions src/core/LoadFileTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ using namespace imageproc;
class LoadFileTask::ErrorResult : public FilterResult {
Q_DECLARE_TR_FUNCTIONS(LoadFileTask)
public:
explicit ErrorResult(const QString& filePath);
explicit ErrorResult(const QString& filePath, const QString& errorDetails = QString());

void updateUI(FilterUiInterface* ui) override;

std::shared_ptr<AbstractFilter> filter() override { return nullptr; }

private:
QString m_filePath;
QString m_errorDetails;
bool m_fileExists;
};

Expand Down Expand Up @@ -71,6 +72,12 @@ FilterResultPtr LoadFileTask::operator()() {
}
} catch (const CancelledException&) {
return nullptr;
} catch (const std::bad_alloc&) {
throw; // Handled by the out-of-memory machinery in WorkerThreadPool.
} catch (const std::exception& e) {
// Without this, the exception would escape the worker thread and abort
// the application via std::terminate().
return std::make_shared<ErrorResult>(m_imageId.filePath(), QString::fromUtf8(e.what()));
}
}

Expand Down Expand Up @@ -108,8 +115,10 @@ void LoadFileTask::convertToSupportedFormat(QImage& image) const {

/*======================= LoadFileTask::ErrorResult ======================*/

LoadFileTask::ErrorResult::ErrorResult(const QString& filePath)
: m_filePath(QDir::toNativeSeparators(filePath)), m_fileExists(QFile::exists(filePath)) {}
LoadFileTask::ErrorResult::ErrorResult(const QString& filePath, const QString& errorDetails)
: m_filePath(QDir::toNativeSeparators(filePath)),
m_errorDetails(errorDetails),
m_fileExists(QFile::exists(filePath)) {}

void LoadFileTask::ErrorResult::updateUI(FilterUiInterface* ui) {
class ErrWidget : public ErrorWidget {
Expand All @@ -128,7 +137,10 @@ void LoadFileTask::ErrorResult::updateUI(FilterUiInterface* ui) {

QString errMsg;
Qt::TextFormat fmt = Qt::AutoText;
if (m_fileExists) {
if (!m_errorDetails.isEmpty()) {
errMsg = tr("An error occurred while processing this page:\n%1\n\nFile: %2").arg(m_errorDetails, m_filePath);
fmt = Qt::PlainText;
} else if (m_fileExists) {
errMsg = tr("The following file could not be loaded:\n%1").arg(m_filePath);
fmt = Qt::PlainText;
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/core/WorkerThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ void WorkerThreadPool::submitTask(const BackgroundTaskPtr& task) {
}
} catch (const std::bad_alloc&) {
OutOfMemoryHandler::instance().handleOutOfMemorySituation();
} catch (const std::exception& e) {
// An exception escaping run() would terminate the application.
qWarning("Background task failed: %s", e.what());
} catch (...) {
qWarning("Background task failed with an unknown exception.");
}
}

Expand Down