mirror of
https://github.com/PrismLauncher/PrismLauncher.git
synced 2025-06-13 05:37:42 +02:00
feat: add sink fail reason and correctly propagate it through the NetRequest
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
@ -58,6 +58,7 @@ class ByteArraySink : public Sink {
|
|||||||
qWarning() << "ByteArraySink did not initialize the buffer because it's not addressable";
|
qWarning() << "ByteArraySink did not initialize the buffer because it's not addressable";
|
||||||
if (initAllValidators(request))
|
if (initAllValidators(request))
|
||||||
return Task::State::Running;
|
return Task::State::Running;
|
||||||
|
m_fail_reason = "Failed to initialize validators";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -69,12 +70,14 @@ class ByteArraySink : public Sink {
|
|||||||
qWarning() << "ByteArraySink did not write the buffer because it's not addressable";
|
qWarning() << "ByteArraySink did not write the buffer because it's not addressable";
|
||||||
if (writeAllValidators(data))
|
if (writeAllValidators(data))
|
||||||
return Task::State::Running;
|
return Task::State::Running;
|
||||||
|
m_fail_reason = "Failed to write validators";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto abort() -> Task::State override
|
auto abort() -> Task::State override
|
||||||
{
|
{
|
||||||
failAllValidators();
|
failAllValidators();
|
||||||
|
m_fail_reason = "Aborted";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,12 +85,13 @@ class ByteArraySink : public Sink {
|
|||||||
{
|
{
|
||||||
if (finalizeAllValidators(reply))
|
if (finalizeAllValidators(reply))
|
||||||
return Task::State::Succeeded;
|
return Task::State::Succeeded;
|
||||||
|
m_fail_reason = "Failed to finalize validators";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto hasLocalData() -> bool override { return false; }
|
auto hasLocalData() -> bool override { return false; }
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
std::shared_ptr<QByteArray> m_output;
|
std::shared_ptr<QByteArray> m_output;
|
||||||
};
|
};
|
||||||
} // namespace Net
|
} // namespace Net
|
||||||
|
@ -51,6 +51,7 @@ Task::State FileSink::init(QNetworkRequest& request)
|
|||||||
// create a new save file and open it for writing
|
// create a new save file and open it for writing
|
||||||
if (!FS::ensureFilePathExists(m_filename)) {
|
if (!FS::ensureFilePathExists(m_filename)) {
|
||||||
qCCritical(taskNetLogC) << "Could not create folder for " + m_filename;
|
qCCritical(taskNetLogC) << "Could not create folder for " + m_filename;
|
||||||
|
m_fail_reason = "Could not create folder";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,11 +59,13 @@ Task::State FileSink::init(QNetworkRequest& request)
|
|||||||
m_output_file.reset(new PSaveFile(m_filename));
|
m_output_file.reset(new PSaveFile(m_filename));
|
||||||
if (!m_output_file->open(QIODevice::WriteOnly)) {
|
if (!m_output_file->open(QIODevice::WriteOnly)) {
|
||||||
qCCritical(taskNetLogC) << "Could not open " + m_filename + " for writing";
|
qCCritical(taskNetLogC) << "Could not open " + m_filename + " for writing";
|
||||||
|
m_fail_reason = "Could not open file";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (initAllValidators(request))
|
if (initAllValidators(request))
|
||||||
return Task::State::Running;
|
return Task::State::Running;
|
||||||
|
m_fail_reason = "Failed to initialize validators";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,6 +76,7 @@ Task::State FileSink::write(QByteArray& data)
|
|||||||
m_output_file->cancelWriting();
|
m_output_file->cancelWriting();
|
||||||
m_output_file.reset();
|
m_output_file.reset();
|
||||||
m_wroteAnyData = false;
|
m_wroteAnyData = false;
|
||||||
|
m_fail_reason = "Failed to write validators";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,13 +109,16 @@ Task::State FileSink::finalize(QNetworkReply& reply)
|
|||||||
if (gotFile || m_wroteAnyData) {
|
if (gotFile || m_wroteAnyData) {
|
||||||
// ask validators for data consistency
|
// ask validators for data consistency
|
||||||
// we only do this for actual downloads, not 'your data is still the same' cache hits
|
// we only do this for actual downloads, not 'your data is still the same' cache hits
|
||||||
if (!finalizeAllValidators(reply))
|
if (!finalizeAllValidators(reply)) {
|
||||||
|
m_fail_reason = "Failed to finalize validators";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
|
}
|
||||||
|
|
||||||
// nothing went wrong...
|
// nothing went wrong...
|
||||||
if (!m_output_file->commit()) {
|
if (!m_output_file->commit()) {
|
||||||
qCCritical(taskNetLogC) << "Failed to commit changes to " << m_filename;
|
qCCritical(taskNetLogC) << "Failed to commit changes to " << m_filename;
|
||||||
m_output_file->cancelWriting();
|
m_output_file->cancelWriting();
|
||||||
|
m_fail_reason = "Failed to commit changes";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,7 +84,8 @@ void NetRequest::executeTask()
|
|||||||
break;
|
break;
|
||||||
case State::Inactive:
|
case State::Inactive:
|
||||||
case State::Failed:
|
case State::Failed:
|
||||||
emit failed("Failed to initialize sink");
|
m_failReason = m_sink->failReason();
|
||||||
|
emit failed(m_sink->failReason());
|
||||||
emit finished();
|
emit finished();
|
||||||
return;
|
return;
|
||||||
case State::AbortedByUser:
|
case State::AbortedByUser:
|
||||||
@ -259,6 +260,7 @@ void NetRequest::downloadFinished()
|
|||||||
} else if (m_state == State::Failed) {
|
} else if (m_state == State::Failed) {
|
||||||
qCDebug(logCat) << getUid().toString() << "Request failed in previous step:" << m_url.toString();
|
qCDebug(logCat) << getUid().toString() << "Request failed in previous step:" << m_url.toString();
|
||||||
m_sink->abort();
|
m_sink->abort();
|
||||||
|
m_failReason = m_reply->errorString();
|
||||||
emit failed(m_reply->errorString());
|
emit failed(m_reply->errorString());
|
||||||
emit finished();
|
emit finished();
|
||||||
return;
|
return;
|
||||||
@ -278,7 +280,8 @@ void NetRequest::downloadFinished()
|
|||||||
if (m_state != State::Succeeded) {
|
if (m_state != State::Succeeded) {
|
||||||
qCDebug(logCat) << getUid().toString() << "Request failed to write:" << m_url.toString();
|
qCDebug(logCat) << getUid().toString() << "Request failed to write:" << m_url.toString();
|
||||||
m_sink->abort();
|
m_sink->abort();
|
||||||
emit failed("failed to write in sink");
|
m_failReason = m_sink->failReason();
|
||||||
|
emit failed(m_sink->failReason());
|
||||||
emit finished();
|
emit finished();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -289,7 +292,8 @@ void NetRequest::downloadFinished()
|
|||||||
if (m_state != State::Succeeded) {
|
if (m_state != State::Succeeded) {
|
||||||
qCDebug(logCat) << getUid().toString() << "Request failed to finalize:" << m_url.toString();
|
qCDebug(logCat) << getUid().toString() << "Request failed to finalize:" << m_url.toString();
|
||||||
m_sink->abort();
|
m_sink->abort();
|
||||||
emit failed("failed to finalize the request");
|
m_failReason = m_sink->failReason();
|
||||||
|
emit failed(m_sink->failReason());
|
||||||
emit finished();
|
emit finished();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -305,7 +309,7 @@ void NetRequest::downloadReadyRead()
|
|||||||
auto data = m_reply->readAll();
|
auto data = m_reply->readAll();
|
||||||
m_state = m_sink->write(data);
|
m_state = m_sink->write(data);
|
||||||
if (m_state == State::Failed) {
|
if (m_state == State::Failed) {
|
||||||
qCCritical(logCat) << getUid().toString() << "Failed to process response chunk";
|
qCCritical(logCat) << getUid().toString() << "Failed to process response chunk:" << m_sink->failReason();
|
||||||
}
|
}
|
||||||
// qDebug() << "Request" << m_url.toString() << "gained" << data.size() << "bytes";
|
// qDebug() << "Request" << m_url.toString() << "gained" << data.size() << "bytes";
|
||||||
} else {
|
} else {
|
||||||
|
@ -52,6 +52,8 @@ class Sink {
|
|||||||
|
|
||||||
virtual auto hasLocalData() -> bool = 0;
|
virtual auto hasLocalData() -> bool = 0;
|
||||||
|
|
||||||
|
QString failReason() const { return m_fail_reason; }
|
||||||
|
|
||||||
void addValidator(Validator* validator)
|
void addValidator(Validator* validator)
|
||||||
{
|
{
|
||||||
if (validator) {
|
if (validator) {
|
||||||
@ -95,5 +97,6 @@ class Sink {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::shared_ptr<Validator>> validators;
|
std::vector<std::shared_ptr<Validator>> validators;
|
||||||
|
QString m_fail_reason;
|
||||||
};
|
};
|
||||||
} // namespace Net
|
} // namespace Net
|
||||||
|
@ -86,6 +86,7 @@ auto ImgurAlbumCreation::Sink::write(QByteArray& data) -> Task::State
|
|||||||
auto ImgurAlbumCreation::Sink::abort() -> Task::State
|
auto ImgurAlbumCreation::Sink::abort() -> Task::State
|
||||||
{
|
{
|
||||||
m_output.clear();
|
m_output.clear();
|
||||||
|
m_fail_reason = "Aborted";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,11 +96,13 @@ auto ImgurAlbumCreation::Sink::finalize(QNetworkReply&) -> Task::State
|
|||||||
QJsonDocument doc = QJsonDocument::fromJson(m_output, &jsonError);
|
QJsonDocument doc = QJsonDocument::fromJson(m_output, &jsonError);
|
||||||
if (jsonError.error != QJsonParseError::NoError) {
|
if (jsonError.error != QJsonParseError::NoError) {
|
||||||
qDebug() << jsonError.errorString();
|
qDebug() << jsonError.errorString();
|
||||||
|
m_fail_reason = "Invalid json reply";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
auto object = doc.object();
|
auto object = doc.object();
|
||||||
if (!object.value("success").toBool()) {
|
if (!object.value("success").toBool()) {
|
||||||
qDebug() << doc.toJson();
|
qDebug() << doc.toJson();
|
||||||
|
m_fail_reason = "Failed to create album";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
m_result->deleteHash = object.value("data").toObject().value("deletehash").toString();
|
m_result->deleteHash = object.value("data").toObject().value("deletehash").toString();
|
||||||
|
@ -90,6 +90,7 @@ auto ImgurUpload::Sink::write(QByteArray& data) -> Task::State
|
|||||||
auto ImgurUpload::Sink::abort() -> Task::State
|
auto ImgurUpload::Sink::abort() -> Task::State
|
||||||
{
|
{
|
||||||
m_output.clear();
|
m_output.clear();
|
||||||
|
m_fail_reason = "Aborted";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,11 +100,13 @@ auto ImgurUpload::Sink::finalize(QNetworkReply&) -> Task::State
|
|||||||
QJsonDocument doc = QJsonDocument::fromJson(m_output, &jsonError);
|
QJsonDocument doc = QJsonDocument::fromJson(m_output, &jsonError);
|
||||||
if (jsonError.error != QJsonParseError::NoError) {
|
if (jsonError.error != QJsonParseError::NoError) {
|
||||||
qDebug() << "imgur server did not reply with JSON" << jsonError.errorString();
|
qDebug() << "imgur server did not reply with JSON" << jsonError.errorString();
|
||||||
|
m_fail_reason = "Invalid json reply";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
auto object = doc.object();
|
auto object = doc.object();
|
||||||
if (!object.value("success").toBool()) {
|
if (!object.value("success").toBool()) {
|
||||||
qDebug() << "Screenshot upload not successful:" << doc.toJson();
|
qDebug() << "Screenshot upload not successful:" << doc.toJson();
|
||||||
|
m_fail_reason = "Screenshot was not uploaded successfully";
|
||||||
return Task::State::Failed;
|
return Task::State::Failed;
|
||||||
}
|
}
|
||||||
m_shot->m_imgurId = object.value("data").toObject().value("id").toString();
|
m_shot->m_imgurId = object.value("data").toObject().value("id").toString();
|
||||||
|
@ -118,10 +118,14 @@ void ConcurrentTask::executeNextSubTask()
|
|||||||
}
|
}
|
||||||
if (m_queue.isEmpty()) {
|
if (m_queue.isEmpty()) {
|
||||||
if (m_doing.isEmpty()) {
|
if (m_doing.isEmpty()) {
|
||||||
if (m_failed.isEmpty())
|
if (m_failed.isEmpty()) {
|
||||||
emitSucceeded();
|
emitSucceeded();
|
||||||
else
|
} else if (m_failed.count() == 1) {
|
||||||
|
auto task = m_failed.keys().first();
|
||||||
|
emitFailed(task->failReason());
|
||||||
|
} else {
|
||||||
emitFailed(tr("One or more subtasks failed"));
|
emitFailed(tr("One or more subtasks failed"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user