Skip to content

Commit

Permalink
Merge pull request #11657 from nextcloud/bugfix/noid/improve-logging
Browse files Browse the repository at this point in the history
fix(federation): Improve logging
  • Loading branch information
nickvergessen authored Feb 27, 2024
2 parents 201e123 + 028e3c8 commit fca0fae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getAvatar(Room $room, Participant $participant, bool $darkTheme)
);

if ($proxy->getStatusCode() !== Http::STATUS_OK) {
$this->proxy->logUnexpectedStatusCode(__METHOD__, $proxy->getStatusCode());
$this->proxy->logUnexpectedStatusCode(__METHOD__, $proxy->getStatusCode(), (string) $proxy->getBody());
throw new CannotReachRemoteException('Avatar request had unexpected status code');
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Federation/Proxy/TalkV1/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function receiveMessages(
// FIXME
// Poor-mans timeout, should later on cancel/trigger earlier,
// when we received a OCM message notifying us about a chat message
sleep($timeout);
sleep(max(0, $timeout - 5));

$proxy = $this->proxy->get(
$participant->getAttendee()->getInvitedCloudId(),
Expand Down
20 changes: 15 additions & 5 deletions lib/Federation/Proxy/TalkV1/ProxyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public function __construct(
/**
* @return Http::STATUS_BAD_REQUEST
*/
public function logUnexpectedStatusCode(string $method, int $statusCode): int {
public function logUnexpectedStatusCode(string $method, int $statusCode, string $logDetails = ''): int {
if ($this->config->getSystemValueBool('debug')) {
$this->logger->error('Unexpected status code ' . $statusCode . ' returned for ' . $method);
$this->logger->error('Unexpected status code ' . $statusCode . ' returned for ' . $method . ($logDetails !== '' ? "\n" . $logDetails : ''));
} else {
$this->logger->debug('Unexpected status code ' . $statusCode . ' returned for ' . $method);
$this->logger->debug('Unexpected status code ' . $statusCode . ' returned for ' . $method . ($logDetails !== '' ? "\n" . $logDetails : ''));
}
return Http::STATUS_BAD_REQUEST;
}
Expand All @@ -78,6 +78,13 @@ protected function generateDefaultRequestOptions(
];
}

protected function prependProtocolIfNotAvailable(string $url): string {
if (!str_starts_with($url, 'http://') && !str_starts_with($url, 'https://')) {
$url = 'https://' . $url;
}
return $url;
}

/**
* @param 'get'|'post'|'put'|'delete' $verb
* @throws CannotReachRemoteException
Expand All @@ -96,7 +103,10 @@ protected function request(
}

try {
return $this->clientService->newClient()->{$verb}($url, $requestOptions);
return $this->clientService->newClient()->{$verb}(
$this->prependProtocolIfNotAvailable($url),
$requestOptions
);
} catch (ClientException $e) {
$status = $e->getResponse()->getStatusCode();

Expand Down Expand Up @@ -212,7 +222,7 @@ public function getOCSData(IResponse $response, array $allowedStatusCodes = [Htt
throw new \RuntimeException('JSON response is not an array');
}
} catch (\Throwable $e) {
$this->logger->error('Error parsing JSON response', ['exception' => $e]);
$this->logger->error('Error parsing JSON response: ' . ($content ?? 'no-data'), ['exception' => $e]);
throw new CannotReachRemoteException('Error parsing JSON response', $e->getCode(), $e);
}

Expand Down

0 comments on commit fca0fae

Please sign in to comment.