Skip to content

Commit

Permalink
feature: added 409 error handling from server response
Browse files Browse the repository at this point in the history
  • Loading branch information
Nethius committed Feb 15, 2025
1 parent 52c1294 commit a1ca994
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 29 deletions.
29 changes: 29 additions & 0 deletions client/core/api/apiUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,32 @@ apiDefs::ConfigSource apiUtils::getConfigSource(const QJsonObject &serverConfigO
{
return static_cast<apiDefs::ConfigSource>(serverConfigObject.value(apiDefs::key::configVersion).toInt());
}

amnezia::ErrorCode apiUtils::checkNetworkReplyErrors(const QList<QSslError> &sslErrors, QNetworkReply *reply)
{
const int httpStatusCodeConflict = 409;

if (!sslErrors.empty()) {
qDebug().noquote() << sslErrors;
return amnezia::ErrorCode::ApiConfigSslError;
} else if (reply->error() == QNetworkReply::NoError) {
return amnezia::ErrorCode::NoError;
} else if (reply->error() == QNetworkReply::NetworkError::OperationCanceledError
|| reply->error() == QNetworkReply::NetworkError::TimeoutError) {
return amnezia::ErrorCode::ApiConfigTimeoutError;
} else {
QString err = reply->errorString();
int httpStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << QString::fromUtf8(reply->readAll());
qDebug() << reply->error();
qDebug() << err;
qDebug() << httpStatusCode;
if (httpStatusCode == httpStatusCodeConflict) {
return amnezia::ErrorCode::ApiConfigLimitError;
}
return amnezia::ErrorCode::ApiConfigDownloadError;
}

qDebug() << "something went wrong";
return amnezia::ErrorCode::InternalError;
}
4 changes: 4 additions & 0 deletions client/core/api/apiUtils.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#ifndef APIUTILS_H
#define APIUTILS_H

#include <QNetworkReply>
#include <QObject>

#include "apiDefs.h"
#include "core/defs.h"

namespace apiUtils
{
Expand All @@ -13,6 +15,8 @@ namespace apiUtils

apiDefs::ConfigType getConfigType(const QJsonObject &serverConfigObject);
apiDefs::ConfigSource getConfigSource(const QJsonObject &serverConfigObject);

amnezia::ErrorCode checkNetworkReplyErrors(const QList<QSslError> &sslErrors, QNetworkReply *reply);
}

#endif // APIUTILS_H
6 changes: 3 additions & 3 deletions client/core/controllers/gatewayController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "QRsa.h"

#include "amnezia_application.h"
#include "core/networkUtilities.h"
#include "core/api/apiUtils.h"
#include "utilities.h"

namespace
Expand Down Expand Up @@ -75,7 +75,7 @@ ErrorCode GatewayController::get(const QString &endpoint, QByteArray &responseBo
bypassProxy(endpoint, reply, requestFunction, replyProcessingFunction);
}

auto errorCode = NetworkUtilities::checkNetworkReplyErrors(sslErrors, reply);
auto errorCode = apiUtils::checkNetworkReplyErrors(sslErrors, reply);
reply->deleteLater();

return errorCode;
Expand Down Expand Up @@ -165,7 +165,7 @@ ErrorCode GatewayController::post(const QString &endpoint, const QJsonObject api
bypassProxy(endpoint, reply, requestFunction, replyProcessingFunction);
}

auto errorCode = NetworkUtilities::checkNetworkReplyErrors(sslErrors, reply);
auto errorCode = apiUtils::checkNetworkReplyErrors(sslErrors, reply);
reply->deleteLater();
if (errorCode) {
return errorCode;
Expand Down
1 change: 1 addition & 0 deletions client/core/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ namespace amnezia
ApiMissingAgwPublicKey = 1105,
ApiConfigDecryptionError = 1106,
ApiServicesMissingError = 1107,
ApiConfigLimitError = 1108,

// QFile errors
OpenError = 1200,
Expand Down
1 change: 1 addition & 0 deletions client/core/errorstrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ QString errorString(ErrorCode code) {
case (ErrorCode::ApiMissingAgwPublicKey): errorMessage = QObject::tr("Missing AGW public key"); break;
case (ErrorCode::ApiConfigDecryptionError): errorMessage = QObject::tr("Failed to decrypt response payload"); break;
case (ErrorCode::ApiServicesMissingError): errorMessage = QObject::tr("Missing list of available services"); break;
case (ErrorCode::ApiConfigLimitError): errorMessage = QObject::tr("The limit of allowed configurations per subscription has been exceeded"); break;

// QFile errors
case(ErrorCode::OpenError): errorMessage = QObject::tr("QFile error: The file could not be opened"); break;
Expand Down
20 changes: 0 additions & 20 deletions client/core/networkUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,6 @@ QStringList NetworkUtilities::summarizeRoutes(const QStringList &ips, const QStr
return QStringList();
}

amnezia::ErrorCode NetworkUtilities::checkNetworkReplyErrors(const QList<QSslError> &sslErrors, QNetworkReply *reply)
{
if (!sslErrors.empty()) {
qDebug().noquote() << sslErrors;
return amnezia::ErrorCode::ApiConfigSslError;
} else if (reply->error() == QNetworkReply::NoError) {
return amnezia::ErrorCode::NoError;
} else if (reply->error() == QNetworkReply::NetworkError::OperationCanceledError
|| reply->error() == QNetworkReply::NetworkError::TimeoutError) {
return amnezia::ErrorCode::ApiConfigTimeoutError;
} else {
QString err = reply->errorString();
qDebug() << QString::fromUtf8(reply->readAll());
qDebug() << reply->error();
qDebug() << err;
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
return amnezia::ErrorCode::ApiConfigDownloadError;
}
}

QString NetworkUtilities::getIPAddress(const QString &host)
{
QHostAddress address(host);
Expand Down
5 changes: 0 additions & 5 deletions client/core/networkUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <QHostAddress>
#include <QNetworkReply>

#include "core/defs.h"


class NetworkUtilities : public QObject
{
Expand All @@ -33,9 +31,6 @@ class NetworkUtilities : public QObject
static QString ipAddressFromIpWithSubnet(const QString ip);

static QStringList summarizeRoutes(const QStringList &ips, const QString cidr);

static amnezia::ErrorCode checkNetworkReplyErrors(const QList<QSslError> &sslErrors, QNetworkReply *reply);

};

#endif // NETWORKUTILITIES_H
2 changes: 1 addition & 1 deletion client/ui/controllers/api/apiConfigsController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ bool ApiConfigsController::updateServiceFromTelegram(const int serverIndex)
connect(reply, &QNetworkReply::sslErrors, [this, &sslErrors](const QList<QSslError> &errors) { sslErrors = errors; });
wait.exec();

auto errorCode = NetworkUtilities::checkNetworkReplyErrors(sslErrors, reply);
auto errorCode = apiUtils::checkNetworkReplyErrors(sslErrors, reply);
if (errorCode != ErrorCode::NoError) {
reply->deleteLater();
emit errorOccurred(errorCode);
Expand Down

0 comments on commit a1ca994

Please sign in to comment.