Use Yggdrasil error response when available, or fall back to legacy HTTP error codes
This commit is contained in:
parent
c700b7be2e
commit
a600286e33
@ -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,27 +131,60 @@ void LoginTask::processReply(QNetworkReply *reply, std::function<void (LoginTask
|
||||
break;
|
||||
|
||||
default:
|
||||
int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
|
||||
switch (responseCode)
|
||||
{
|
||||
case 403:
|
||||
emitFailed(tr("Invalid username or password."));
|
||||
break;
|
||||
|
||||
case 503:
|
||||
emitFailed(tr("The login servers are currently unavailable. Check "
|
||||
"http://help.mojang.com/ for more info."));
|
||||
break;
|
||||
|
||||
default:
|
||||
QLOG_DEBUG() << "Login failed with QNetworkReply code:" << reply->error();
|
||||
emitFailed(tr("Login failed: %1").arg(reply->errorString()));
|
||||
break;
|
||||
}
|
||||
emitFailed(errorHandler(this, reply));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
QString LoginTask::parseLegacyError(QNetworkReply *reply)
|
||||
{
|
||||
int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
|
||||
switch (responseCode)
|
||||
{
|
||||
case 403:
|
||||
return tr("Invalid username or password.");
|
||||
|
||||
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()
|
||||
{
|
||||
setStatus(tr("Logging in..."));
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user