Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix invalid notification title in Imgur failed uploads #803

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

Merged
Merged
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
28 changes: 26 additions & 2 deletions src/backend/uploader/imgur/ImgurUploader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,34 @@ QString ImgurUploader::formatResponseUrl(const ImgurResponse &response) const
return response.link();
}

void ImgurUploader::imgurError(const QString &message)
void ImgurUploader::imgurError(QNetworkReply::NetworkError networkError, const QString &message)
{
qCritical("MainWindow: Imgur uploader returned error: '%s'", qPrintable(message));
emit finished(UploadResult(UploadStatus::NoError, type(), message));
emit finished(UploadResult(mapErrorTypeToStatus(networkError), type(), message));
}

UploadStatus ImgurUploader::mapErrorTypeToStatus(QNetworkReply::NetworkError errorType)
{
switch (errorType) {
case QNetworkReply::NetworkError::NoError:
return UploadStatus::NoError;
case QNetworkReply::NetworkError::TimeoutError:
return UploadStatus::TimedOut;
case QNetworkReply::NetworkError::ConnectionRefusedError:
case QNetworkReply::NetworkError::RemoteHostClosedError:
case QNetworkReply::NetworkError::HostNotFoundError:
case QNetworkReply::NetworkError::TemporaryNetworkFailureError:
case QNetworkReply::NetworkError::ServiceUnavailableError:
return UploadStatus::ConnectionError;
case QNetworkReply::NetworkError::ContentOperationNotPermittedError:
case QNetworkReply::NetworkError::ContentAccessDenied:
case QNetworkReply::NetworkError::AuthenticationRequiredError:
return UploadStatus::PermissionError;
case QNetworkReply::ProtocolFailure:
return UploadStatus::WebError;
default:
return UploadStatus::UnknownError;
}
}

void ImgurUploader::imgurTokenUpdated(const QString &accessToken, const QString &refreshToken, const QString &username)
Expand Down
4 changes: 3 additions & 1 deletion src/backend/uploader/imgur/ImgurUploader.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ Q_OBJECT
ImgurResponseLogger *mImgurResponseLogger;
QImage mImage;

static UploadStatus mapErrorTypeToStatus(QNetworkReply::NetworkError errorType);

private slots:
void imgurUploadFinished(const ImgurResponse &response);
void imgurError(const QString &message);
void imgurError(QNetworkReply::NetworkError networkError, const QString &message);
void imgurTokenUpdated(const QString &accessToken, const QString &refreshToken, const QString &username);
void imgurTokenRefresh();
QString formatResponseUrl(const ImgurResponse &response) const;
Expand Down
26 changes: 13 additions & 13 deletions src/backend/uploader/imgur/ImgurWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ void ImgurWrapper::handleDataResponse(const QDomElement& element) const
emit tokenRefreshRequired();
} else {
if (element.elementsByTagName(QLatin1String("error")).isEmpty()) {
emit error(QLatin1String("Server responded with ") + element.attribute(QLatin1String("status")));
emit error(QNetworkReply::ProtocolFailure, QLatin1String("Server responded with ") + element.attribute(QLatin1String("status")));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pretty sure ProtocolFailure is the best option for these errors.

Copy link
Member

Choose a reason for hiding this comment

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

Sound good I think.

Copy link
Member

Choose a reason for hiding this comment

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

Though I'm not sure if we should pass the QNetworkReply outside the Warpper. The idea of the wrapper is to hide the implementation, the upload could happen via network or be send via pigeon, the caller shouldn't know about it. Is there a way to not pass this enum outside the the Warpper? Haven't had time to look into it yet, I can check it over the weekend.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't think of another way though 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Looking at the code, is the error type that much relevant? In the path with the settings we ignore it completely. From my point of view the issue was UploadStatus::NoError in the imgurError method, there it should have been something like UploadStatus::WebError. How about moving the mapping method into the ImgurWrapper class and formatting the error string based on the type and only returning the string again. So or so we only forward this information to the notification service and show it to the user, we have no special treatment based on type. What do you think?

} else {
emit error(QLatin1String("Server responded with ") + element.attribute(QLatin1String("status")) + ": " +
emit error(QNetworkReply::ProtocolFailure, QLatin1String("Server responded with ") + element.attribute(QLatin1String("status")) + ": " +
element.elementsByTagName(QLatin1String("error")).at(0).toElement().text());
}
}
Expand All @@ -180,7 +180,7 @@ void ImgurWrapper::handleTokenResponse(const QDomElement& element) const
element.elementsByTagName(QLatin1String("account_username")).at(0).toElement().text()
);
} else {
emit error(QLatin1String("Expected token response was received, something went wrong."));
emit error(QNetworkReply::ProtocolFailure, QLatin1String("Expected token response was received, something went wrong."));
}
}

Expand All @@ -203,7 +203,7 @@ void ImgurWrapper::handleReply(QNetworkReply* reply)
// token.
if (reply->error() != QNetworkReply::NoError &&
reply->error() != QNetworkReply::ContentOperationNotPermittedError) {
emit error(QLatin1String("Network Error(") + QString::number(reply->error()) + "): " + reply->errorString());
emit error(reply->error(), QLatin1String("Network Error(") + QString::number(reply->error()) + "): " + reply->errorString());
reply->deleteLater();
return;
}
Expand All @@ -213,13 +213,14 @@ void ImgurWrapper::handleReply(QNetworkReply* reply)
int errorLine;
int errorColumn;

// Try to parse reply into xml reader
if (!doc.setContent(reply->readAll(), false, &errorMessage, &errorLine, &errorColumn)) {
emit error(QLatin1String("Parse error: ") + errorMessage + QLatin1String(", line:") + errorLine +
QLatin1String(", column:") + errorColumn);
reply->deleteLater();
return;
}
// Try to parse reply into xml reader
if (!doc.setContent(reply->readAll(), false, &errorMessage, &errorLine, &errorColumn)) {
emit error(QNetworkReply::ProtocolFailure,
QLatin1String("Parse error: ") + errorMessage + QLatin1String(", line:") + errorLine +
QLatin1String(", column:") + errorColumn);
reply->deleteLater();
return;
}

// See if we have an upload reply, token response or error
auto rootElement = doc.documentElement();
Expand All @@ -229,9 +230,8 @@ void ImgurWrapper::handleReply(QNetworkReply* reply)
} else if (rootElement.tagName() == QLatin1String("response")) {
handleTokenResponse(rootElement);
}

else {
emit error(QLatin1String("Received unexpected reply from imgur server."));
emit error(QNetworkReply::ProtocolFailure, QLatin1String("Received unexpected reply from imgur server."));
}

reply->deleteLater();
Expand Down
8 changes: 4 additions & 4 deletions src/backend/uploader/imgur/ImgurWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class ImgurWrapper : public QObject
QUrl pinRequestUrl(const QString &clientId) const;

signals:
void uploadFinished(const ImgurResponse &response) const;
void error(const QString &message) const;
void tokenUpdated(const QString &accessToken, const QString &refreshToken, const QString &username) const;
void tokenRefreshRequired() const;
void uploadFinished(const ImgurResponse &response) const;
void error(QNetworkReply::NetworkError networkError, const QString &message) const;
void tokenUpdated(const QString &accessToken, const QString &refreshToken, const QString &username) const;
void tokenRefreshRequired() const;

private:
QNetworkAccessManager *mAccessManager;
Expand Down
3 changes: 2 additions & 1 deletion src/gui/settingsDialog/uploader/ImgurUploaderSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ void ImgurUploaderSettings::imgurTokenUpdated(const QString& accessToken, const
* Something went wrong while requesting a new token, we write the message to
* shell.
*/
void ImgurUploaderSettings::imgurTokenError(const QString& message)
void ImgurUploaderSettings::imgurTokenError(QNetworkReply::NetworkError networkError, const QString& message)
{
Q_UNUSED(networkError);
qCritical("SettingsDialog returned error: '%s'", qPrintable(message));
qInfo("%s", qPrintable(tr("Imgur.com token update error.")));
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/settingsDialog/uploader/ImgurUploaderSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private slots:
void clearImgurToken();
void imgurClientEntered(const QString &text);
void imgurTokenUpdated(const QString &accessToken, const QString &refreshToken, const QString &username);
void imgurTokenError(const QString &message);
void imgurTokenError(QNetworkReply::NetworkError networkError, const QString &message);
void showImgurHistoryDialog();
void usernameChanged();
};
Expand Down