summaryrefslogtreecommitdiffstats
path: root/logic/net
diff options
context:
space:
mode:
authorSky <git@bunnies.cc>2013-10-17 00:46:25 +0100
committerSky <git@bunnies.cc>2013-10-17 00:46:25 +0100
commita600286e33601a85949b9e51bd5421a45f9998ac (patch)
treefb881cc10184ff15850136276706913ef352e5ad /logic/net
parentc700b7be2e6e5632f6cea6f0c45744f494a0629f (diff)
downloadMultiMC-a600286e33601a85949b9e51bd5421a45f9998ac.tar
MultiMC-a600286e33601a85949b9e51bd5421a45f9998ac.tar.gz
MultiMC-a600286e33601a85949b9e51bd5421a45f9998ac.tar.lz
MultiMC-a600286e33601a85949b9e51bd5421a45f9998ac.tar.xz
MultiMC-a600286e33601a85949b9e51bd5421a45f9998ac.zip
Use Yggdrasil error response when available, or fall back to legacy HTTP error codes
Diffstat (limited to 'logic/net')
-rw-r--r--logic/net/LoginTask.cpp69
-rw-r--r--logic/net/LoginTask.h4
2 files changed, 54 insertions, 19 deletions
diff --git a/logic/net/LoginTask.cpp b/logic/net/LoginTask.cpp
index 70ef2ae6..4098783b 100644
--- a/logic/net/LoginTask.cpp
+++ b/logic/net/LoginTask.cpp
@@ -92,15 +92,15 @@ void LoginTask::parseLegacyReply(QByteArray data)
void LoginTask::processLegacyReply(QNetworkReply *reply)
{
- processReply(reply, &LoginTask::parseLegacyReply);
+ processReply(reply, &LoginTask::parseLegacyReply, &LoginTask::parseLegacyError);
}
void LoginTask::processYggdrasilReply(QNetworkReply *reply)
{
- processReply(reply, &LoginTask::parseYggdrasilReply);
+ processReply(reply, &LoginTask::parseYggdrasilReply, &LoginTask::parseYggdrasilError);
}
-void LoginTask::processReply(QNetworkReply *reply, std::function<void (LoginTask*, QByteArray)> parser)
+void LoginTask::processReply(QNetworkReply *reply, std::function<void (LoginTask*, QByteArray)> parser, std::function<QString (LoginTask*, QNetworkReply*)> errorHandler)
{
if (netReply != reply)
return;
@@ -131,25 +131,58 @@ void LoginTask::processReply(QNetworkReply *reply, std::function<void (LoginTask
break;
default:
- int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
+ emitFailed(errorHandler(this, reply));
+ break;
+ }
+}
- switch (responseCode)
- {
- case 403:
- emitFailed(tr("Invalid username or password."));
- break;
+QString LoginTask::parseLegacyError(QNetworkReply *reply)
+{
+ int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
- case 503:
- emitFailed(tr("The login servers are currently unavailable. Check "
- "http://help.mojang.com/ for more info."));
- break;
+ switch (responseCode)
+ {
+ case 403:
+ return tr("Invalid username or password.");
- default:
- QLOG_DEBUG() << "Login failed with QNetworkReply code:" << reply->error();
- emitFailed(tr("Login failed: %1").arg(reply->errorString()));
- break;
- }
+ case 503:
+ return tr("The login servers are currently unavailable. Check "
+ "http://help.mojang.com/ for more info.");
+
+ default:
+ QLOG_DEBUG() << "Login failed with QNetworkReply code:" << reply->error();
+ return tr("Login failed: %1").arg(reply->errorString());
+ }
+}
+
+QString LoginTask::parseYggdrasilError(QNetworkReply *reply)
+{
+ QByteArray data = reply->readAll();
+ QJsonParseError jsonError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
+
+ // If there are JSON errors fall back to using the legacy error handling using HTTP status codes
+ if (jsonError.error != QJsonParseError::NoError)
+ {
+ return parseLegacyError(reply);
+ }
+
+ if (!jsonDoc.isObject())
+ {
+ return parseLegacyError(reply);
}
+
+ QJsonObject root = jsonDoc.object();
+
+ //QString error = root.value("error").toString();
+ QString errorMessage = root.value("errorMessage").toString();
+
+ if(errorMessage.isEmpty())
+ {
+ return parseLegacyError(reply);
+ }
+
+ return tr("Login failed: ") + errorMessage;
}
void LoginTask::yggdrasilLogin()
diff --git a/logic/net/LoginTask.h b/logic/net/LoginTask.h
index fa5897cb..aa925999 100644
--- a/logic/net/LoginTask.h
+++ b/logic/net/LoginTask.h
@@ -49,12 +49,14 @@ protected slots:
void legacyLogin();
void processLegacyReply(QNetworkReply *reply);
void parseLegacyReply(QByteArray data);
+ QString parseLegacyError(QNetworkReply *reply);
void yggdrasilLogin();
void processYggdrasilReply(QNetworkReply *reply);
void parseYggdrasilReply(QByteArray data);
+ QString parseYggdrasilError(QNetworkReply *reply);
- void processReply(QNetworkReply *reply, std::function<void(LoginTask*, QByteArray)>);
+ void processReply(QNetworkReply *reply, std::function<void(LoginTask*, QByteArray)>, std::function<QString(LoginTask*, QNetworkReply*)>);
protected:
void executeTask();