Skip to content

Commit 3a94ba5

Browse files
authored
Add more logging on http responses - specifically in error scenarios (#12059)
1 parent abfe443 commit 3a94ba5

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/libsync/httplogger.cpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ void logHttp(const QByteArray &verb, HttpContext *ctx, QJsonObject &&header, QIO
6868
{
6969
static const bool redact = !qEnvironmentVariableIsSet("OWNCLOUD_HTTPLOGGER_NO_REDACT");
7070
const auto reply = qobject_cast<QNetworkReply *>(device);
71-
const auto contentLength = device ? device->size() : 0;
7271

7372
if (redact) {
7473
const QString authKey = QStringLiteral("Authorization");
@@ -103,7 +102,8 @@ void logHttp(const QByteArray &verb, HttpContext *ctx, QJsonObject &&header, QIO
103102
info.insert(QStringLiteral("cached"), cached);
104103
}
105104

106-
QJsonObject body = {{QStringLiteral("length"), contentLength}};
105+
const auto contentLength = device ? device->size() : 0;
106+
QJsonObject body = {{QStringLiteral("length"), device ? QString::number(contentLength) : QStringLiteral("null")}};
107107
if (contentLength > 0) {
108108
QString contentType = header.value(QStringLiteral("Content-Type")).toString();
109109
if (contentType.isEmpty()) {
@@ -146,19 +146,24 @@ void HttpLogger::logRequest(QNetworkReply *reply, QNetworkAccessManager::Operati
146146

147147
auto ctx = std::make_unique<HttpContext>(reply->request());
148148

149+
const auto logError = [reply, operation, id = ctx->id]() {
150+
auto url = reply->request().url();
151+
qCInfo(lcNetworkHttp).noquote().nospace() << "An error occurred for " << url.toDisplayString() << ": " << reply->errorString() << " (" << reply->error()
152+
<< ", " << operation << "), request-id: " << id;
153+
};
154+
149155
// device should still exist, lets still use a qpointer to ensure we have valid data
150-
const auto logSend = [ctx = ctx.get(), operation, reply, device = QPointer<QIODevice>(device), deviceRaw = device](bool cached = false) {
156+
const auto logSend = [ctx = ctx.get(), operation, reply, device = QPointer<QIODevice>(device), deviceRaw = device, logError](bool cached = false) {
151157
Q_ASSERT(!deviceRaw || device);
152158
if (!ctx->send) {
153159
ctx->send = true;
154160
ctx->timer.reset();
155-
} else {
161+
} else if (ctx->lastUrl != reply->url()) {
156162
// this is a redirect
157-
if (ctx->lastUrl != reply->url()) {
158-
ctx->addRedirect(reply->url());
159-
} else {
160-
Q_UNREACHABLE();
161-
}
163+
ctx->addRedirect(reply->url());
164+
} else {
165+
// Probably an error (time-out?).
166+
logError();
162167
}
163168

164169
const auto request = reply->request();
@@ -169,6 +174,7 @@ void HttpLogger::logRequest(QNetworkReply *reply, QNetworkAccessManager::Operati
169174
logHttp(requestVerb(operation, request), ctx, std::move(header), device, cached);
170175
};
171176
QObject::connect(reply, &QNetworkReply::requestSent, reply, logSend, Qt::DirectConnection);
177+
QObject::connect(reply, &QNetworkReply::errorOccurred, reply, logError, Qt::DirectConnection);
172178

173179

174180
QObject::connect(

src/libsync/syncengine.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ void SyncEngine::abort(const QString &reason)
819819
disconnect(_discoveryPhase.get(), nullptr, this, nullptr);
820820
}
821821
if (aborting) {
822-
qCInfo(lcEngine) << "Aborting sync";
822+
qCInfo(lcEngine) << "Aborting sync, stated reason:" << reason;
823823
if (!_goingDown) {
824824
Q_EMIT syncError(tr("Aborted due to %1").arg(reason));
825825
}

0 commit comments

Comments
 (0)