mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-11-13 05:22:49 -05:00
fix: Getting http errors (#252)
This commit is contained in:
@ -371,7 +371,7 @@ ChatRootView {
|
||||
id: errorToast
|
||||
z: 1000
|
||||
|
||||
color: Qt.rgba(0.8, 0.2, 0.2, 0.7)
|
||||
color: Qt.rgba(0.8, 0.2, 0.2, 0.9)
|
||||
border.color: Qt.darker(infoToast.color, 1.3)
|
||||
toastTextColor: "#FFFFFF"
|
||||
}
|
||||
@ -380,7 +380,7 @@ ChatRootView {
|
||||
id: infoToast
|
||||
z: 1000
|
||||
|
||||
color: Qt.rgba(0.2, 0.8, 0.2, 0.7)
|
||||
color: Qt.rgba(0.2, 0.8, 0.2, 0.9)
|
||||
border.color: Qt.darker(infoToast.color, 1.3)
|
||||
toastTextColor: "#FFFFFF"
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ Rectangle {
|
||||
property alias toastTextColor: textItem.color
|
||||
|
||||
property string errorText: ""
|
||||
property int displayDuration: 5000
|
||||
property int displayDuration: 7000
|
||||
|
||||
width: Math.min(parent.width - 40, textItem.implicitWidth + radius)
|
||||
height: visible ? (textItem.implicitHeight + 12) : 0
|
||||
|
||||
@ -60,9 +60,15 @@ void HttpClient::onSendRequest(const HttpRequest &request)
|
||||
void HttpClient::onReadyRead()
|
||||
{
|
||||
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
|
||||
|
||||
if (!reply || reply->isFinished())
|
||||
return;
|
||||
|
||||
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (statusCode >= 400) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString requestId;
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
@ -94,11 +100,17 @@ void HttpClient::onFinished()
|
||||
if (!reply)
|
||||
return;
|
||||
|
||||
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
QByteArray responseBody = reply->readAll();
|
||||
QNetworkReply::NetworkError networkError = reply->error();
|
||||
QString networkErrorString = reply->errorString();
|
||||
|
||||
reply->disconnect();
|
||||
|
||||
QString requestId;
|
||||
bool hasError = false;
|
||||
QString errorMsg;
|
||||
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
bool found = false;
|
||||
@ -116,9 +128,23 @@ void HttpClient::onFinished()
|
||||
return;
|
||||
}
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
hasError = true;
|
||||
errorMsg = reply->errorString();
|
||||
hasError = (networkError != QNetworkReply::NoError) || (statusCode >= 400);
|
||||
|
||||
if (hasError) {
|
||||
errorMsg = parseErrorFromResponse(statusCode, responseBody, networkErrorString);
|
||||
}
|
||||
|
||||
LOG_MESSAGE(QString("HttpClient: Request %1 - HTTP Status: %2").arg(requestId).arg(statusCode));
|
||||
|
||||
if (!responseBody.isEmpty()) {
|
||||
LOG_MESSAGE(QString("HttpClient: Request %1 - Response body (%2 bytes): %3")
|
||||
.arg(requestId)
|
||||
.arg(responseBody.size())
|
||||
.arg(QString::fromUtf8(responseBody)));
|
||||
}
|
||||
|
||||
if (hasError) {
|
||||
LOG_MESSAGE(QString("HttpClient: Request %1 - Error: %2").arg(requestId, errorMsg));
|
||||
}
|
||||
}
|
||||
|
||||
@ -137,6 +163,39 @@ QString HttpClient::addActiveRequest(QNetworkReply *reply, const QString &reques
|
||||
return requestId;
|
||||
}
|
||||
|
||||
QString HttpClient::parseErrorFromResponse(
|
||||
int statusCode, const QByteArray &responseBody, const QString &networkErrorString)
|
||||
{
|
||||
QString errorMsg;
|
||||
|
||||
if (!responseBody.isEmpty()) {
|
||||
QJsonDocument errorDoc = QJsonDocument::fromJson(responseBody);
|
||||
if (!errorDoc.isNull() && errorDoc.isObject()) {
|
||||
QJsonObject errorObj = errorDoc.object();
|
||||
if (errorObj.contains("error")) {
|
||||
QJsonObject error = errorObj["error"].toObject();
|
||||
QString message = error["message"].toString();
|
||||
QString type = error["type"].toString();
|
||||
QString code = error["code"].toString();
|
||||
|
||||
errorMsg = QString("HTTP %1: %2").arg(statusCode).arg(message);
|
||||
if (!type.isEmpty())
|
||||
errorMsg += QString(" (type: %1)").arg(type);
|
||||
if (!code.isEmpty())
|
||||
errorMsg += QString(" (code: %1)").arg(code);
|
||||
} else {
|
||||
errorMsg = QString("HTTP %1: %2").arg(statusCode).arg(QString::fromUtf8(responseBody));
|
||||
}
|
||||
} else {
|
||||
errorMsg = QString("HTTP %1: %2").arg(statusCode).arg(QString::fromUtf8(responseBody));
|
||||
}
|
||||
} else {
|
||||
errorMsg = QString("HTTP %1: %2").arg(statusCode).arg(networkErrorString);
|
||||
}
|
||||
|
||||
return errorMsg;
|
||||
}
|
||||
|
||||
void HttpClient::cancelRequest(const QString &requestId)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
|
||||
@ -59,6 +59,7 @@ private slots:
|
||||
|
||||
private:
|
||||
QString addActiveRequest(QNetworkReply *reply, const QString &requestId);
|
||||
QString parseErrorFromResponse(int statusCode, const QByteArray &responseBody, const QString &networkErrorString);
|
||||
|
||||
QNetworkAccessManager *m_manager;
|
||||
QHash<QString, QNetworkReply *> m_activeRequests;
|
||||
|
||||
Reference in New Issue
Block a user