Use Yggdrasil error response when available, or fall back to legacy HTTP error codes

This commit is contained in:
Sky 2013-10-17 00:46:25 +01:00
parent c700b7be2e
commit a600286e33
2 changed files with 57 additions and 22 deletions

View File

@ -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:
emitFailed(errorHandler(this, reply));
break;
}
}
QString LoginTask::parseLegacyError(QNetworkReply *reply)
{
int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
switch (responseCode)
{
case 403:
emitFailed(tr("Invalid username or password."));
break;
return tr("Invalid username or password.");
case 503:
emitFailed(tr("The login servers are currently unavailable. Check "
"http://help.mojang.com/ for more info."));
break;
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();
emitFailed(tr("Login failed: %1").arg(reply->errorString()));
break;
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()

View File

@ -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();