Fixed double JSON callback when http error occurs

This commit is contained in:
Atlante45 2014-04-07 14:35:13 -07:00
parent e85703d25b
commit 262c6d3206
2 changed files with 19 additions and 12 deletions

View file

@ -179,15 +179,23 @@ void AccountManager::invokedRequest(const QString& path, QNetworkAccessManager::
// if we ended up firing of a request, hook up to it now // if we ended up firing of a request, hook up to it now
connect(networkReply, SIGNAL(finished()), connect(networkReply, SIGNAL(finished()),
SLOT(passSuccessToCallback())); SLOT(processReply()));
connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
SLOT(passErrorToCallback(QNetworkReply::NetworkError)));
} }
} }
} }
void AccountManager::passSuccessToCallback() { void AccountManager::processReply() {
QNetworkReply* requestReply = reinterpret_cast<QNetworkReply*>(sender()); QNetworkReply* requestReply = reinterpret_cast<QNetworkReply*>(sender());
if (requestReply->error() == QNetworkReply::NoError) {
passSuccessToCallback(requestReply);
} else {
passErrorToCallback(requestReply);
}
delete requestReply;
}
void AccountManager::passSuccessToCallback(QNetworkReply* requestReply) {
QJsonDocument jsonResponse = QJsonDocument::fromJson(requestReply->readAll()); QJsonDocument jsonResponse = QJsonDocument::fromJson(requestReply->readAll());
JSONCallbackParameters callbackParams = _pendingCallbackMap.value(requestReply); JSONCallbackParameters callbackParams = _pendingCallbackMap.value(requestReply);
@ -206,17 +214,15 @@ void AccountManager::passSuccessToCallback() {
qDebug() << jsonResponse; qDebug() << jsonResponse;
} }
} }
delete requestReply;
} }
void AccountManager::passErrorToCallback(QNetworkReply::NetworkError errorCode) { void AccountManager::passErrorToCallback(QNetworkReply* requestReply) {
QNetworkReply* requestReply = reinterpret_cast<QNetworkReply*>(sender());
JSONCallbackParameters callbackParams = _pendingCallbackMap.value(requestReply); JSONCallbackParameters callbackParams = _pendingCallbackMap.value(requestReply);
if (callbackParams.errorCallbackReceiver) { if (callbackParams.errorCallbackReceiver) {
// invoke the right method on the callback receiver // invoke the right method on the callback receiver
QMetaObject::invokeMethod(callbackParams.errorCallbackReceiver, qPrintable(callbackParams.errorCallbackMethod), QMetaObject::invokeMethod(callbackParams.errorCallbackReceiver, qPrintable(callbackParams.errorCallbackMethod),
Q_ARG(QNetworkReply::NetworkError, errorCode), Q_ARG(QNetworkReply::NetworkError, requestReply->error()),
Q_ARG(const QString&, requestReply->errorString())); Q_ARG(const QString&, requestReply->errorString()));
// remove the related reply-callback group from the map // remove the related reply-callback group from the map
@ -224,10 +230,9 @@ void AccountManager::passErrorToCallback(QNetworkReply::NetworkError errorCode)
} else { } else {
if (VERBOSE_HTTP_REQUEST_DEBUGGING) { if (VERBOSE_HTTP_REQUEST_DEBUGGING) {
qDebug() << "Received error response from data-server that has no matching callback."; qDebug() << "Received error response from data-server that has no matching callback.";
qDebug() << "Error" << errorCode << "-" << requestReply->errorString(); qDebug() << "Error" << requestReply->error() << "-" << requestReply->errorString();
} }
} }
delete requestReply;
} }
bool AccountManager::hasValidAccessToken() { bool AccountManager::hasValidAccessToken() {

View file

@ -73,13 +73,15 @@ signals:
void loginComplete(const QUrl& authURL); void loginComplete(const QUrl& authURL);
void logoutComplete(); void logoutComplete();
private slots: private slots:
void passSuccessToCallback(); void processReply();
void passErrorToCallback(QNetworkReply::NetworkError errorCode);
private: private:
AccountManager(); AccountManager();
AccountManager(AccountManager const& other); // not implemented AccountManager(AccountManager const& other); // not implemented
void operator=(AccountManager const& other); // not implemented void operator=(AccountManager const& other); // not implemented
void passSuccessToCallback(QNetworkReply* reply);
void passErrorToCallback(QNetworkReply* reply);
Q_INVOKABLE void invokedRequest(const QString& path, QNetworkAccessManager::Operation operation, Q_INVOKABLE void invokedRequest(const QString& path, QNetworkAccessManager::Operation operation,
const JSONCallbackParameters& callbackParams, const JSONCallbackParameters& callbackParams,
const QByteArray& dataByteArray, const QByteArray& dataByteArray,