feature: added 409 error handling from server response

This commit is contained in:
vladimir.kuznetsov 2025-02-15 13:58:48 +07:00
parent 52c12940c4
commit a1ca994c8b
8 changed files with 39 additions and 29 deletions

View file

@ -49,3 +49,32 @@ apiDefs::ConfigSource apiUtils::getConfigSource(const QJsonObject &serverConfigO
{ {
return static_cast<apiDefs::ConfigSource>(serverConfigObject.value(apiDefs::key::configVersion).toInt()); 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;
}

View file

@ -1,9 +1,11 @@
#ifndef APIUTILS_H #ifndef APIUTILS_H
#define APIUTILS_H #define APIUTILS_H
#include <QNetworkReply>
#include <QObject> #include <QObject>
#include "apiDefs.h" #include "apiDefs.h"
#include "core/defs.h"
namespace apiUtils namespace apiUtils
{ {
@ -13,6 +15,8 @@ namespace apiUtils
apiDefs::ConfigType getConfigType(const QJsonObject &serverConfigObject); apiDefs::ConfigType getConfigType(const QJsonObject &serverConfigObject);
apiDefs::ConfigSource getConfigSource(const QJsonObject &serverConfigObject); apiDefs::ConfigSource getConfigSource(const QJsonObject &serverConfigObject);
amnezia::ErrorCode checkNetworkReplyErrors(const QList<QSslError> &sslErrors, QNetworkReply *reply);
} }
#endif // APIUTILS_H #endif // APIUTILS_H

View file

@ -9,7 +9,7 @@
#include "QRsa.h" #include "QRsa.h"
#include "amnezia_application.h" #include "amnezia_application.h"
#include "core/networkUtilities.h" #include "core/api/apiUtils.h"
#include "utilities.h" #include "utilities.h"
namespace namespace
@ -75,7 +75,7 @@ ErrorCode GatewayController::get(const QString &endpoint, QByteArray &responseBo
bypassProxy(endpoint, reply, requestFunction, replyProcessingFunction); bypassProxy(endpoint, reply, requestFunction, replyProcessingFunction);
} }
auto errorCode = NetworkUtilities::checkNetworkReplyErrors(sslErrors, reply); auto errorCode = apiUtils::checkNetworkReplyErrors(sslErrors, reply);
reply->deleteLater(); reply->deleteLater();
return errorCode; return errorCode;
@ -165,7 +165,7 @@ ErrorCode GatewayController::post(const QString &endpoint, const QJsonObject api
bypassProxy(endpoint, reply, requestFunction, replyProcessingFunction); bypassProxy(endpoint, reply, requestFunction, replyProcessingFunction);
} }
auto errorCode = NetworkUtilities::checkNetworkReplyErrors(sslErrors, reply); auto errorCode = apiUtils::checkNetworkReplyErrors(sslErrors, reply);
reply->deleteLater(); reply->deleteLater();
if (errorCode) { if (errorCode) {
return errorCode; return errorCode;

View file

@ -109,6 +109,7 @@ namespace amnezia
ApiMissingAgwPublicKey = 1105, ApiMissingAgwPublicKey = 1105,
ApiConfigDecryptionError = 1106, ApiConfigDecryptionError = 1106,
ApiServicesMissingError = 1107, ApiServicesMissingError = 1107,
ApiConfigLimitError = 1108,
// QFile errors // QFile errors
OpenError = 1200, OpenError = 1200,

View file

@ -66,6 +66,7 @@ QString errorString(ErrorCode code) {
case (ErrorCode::ApiMissingAgwPublicKey): errorMessage = QObject::tr("Missing AGW public key"); break; 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::ApiConfigDecryptionError): errorMessage = QObject::tr("Failed to decrypt response payload"); break;
case (ErrorCode::ApiServicesMissingError): errorMessage = QObject::tr("Missing list of available services"); 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 // QFile errors
case(ErrorCode::OpenError): errorMessage = QObject::tr("QFile error: The file could not be opened"); break; case(ErrorCode::OpenError): errorMessage = QObject::tr("QFile error: The file could not be opened"); break;

View file

@ -107,26 +107,6 @@ QStringList NetworkUtilities::summarizeRoutes(const QStringList &ips, const QStr
return QStringList(); 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) QString NetworkUtilities::getIPAddress(const QString &host)
{ {
QHostAddress address(host); QHostAddress address(host);

View file

@ -7,8 +7,6 @@
#include <QHostAddress> #include <QHostAddress>
#include <QNetworkReply> #include <QNetworkReply>
#include "core/defs.h"
class NetworkUtilities : public QObject class NetworkUtilities : public QObject
{ {
@ -33,9 +31,6 @@ public:
static QString ipAddressFromIpWithSubnet(const QString ip); static QString ipAddressFromIpWithSubnet(const QString ip);
static QStringList summarizeRoutes(const QStringList &ips, const QString cidr); static QStringList summarizeRoutes(const QStringList &ips, const QString cidr);
static amnezia::ErrorCode checkNetworkReplyErrors(const QList<QSslError> &sslErrors, QNetworkReply *reply);
}; };
#endif // NETWORKUTILITIES_H #endif // NETWORKUTILITIES_H

View file

@ -269,7 +269,7 @@ bool ApiConfigsController::updateServiceFromTelegram(const int serverIndex)
connect(reply, &QNetworkReply::sslErrors, [this, &sslErrors](const QList<QSslError> &errors) { sslErrors = errors; }); connect(reply, &QNetworkReply::sslErrors, [this, &sslErrors](const QList<QSslError> &errors) { sslErrors = errors; });
wait.exec(); wait.exec();
auto errorCode = NetworkUtilities::checkNetworkReplyErrors(sslErrors, reply); auto errorCode = apiUtils::checkNetworkReplyErrors(sslErrors, reply);
if (errorCode != ErrorCode::NoError) { if (errorCode != ErrorCode::NoError) {
reply->deleteLater(); reply->deleteLater();
emit errorOccurred(errorCode); emit errorOccurred(errorCode);