Skip to content

Commit

Permalink
fix(federation): Allow federation error responses to be read twice
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Nov 22, 2024
1 parent ad5f9a5 commit 1c7b81f
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/Federation/Proxy/TalkV1/ProxyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ protected function request(
$requestOptions
);
} catch (ClientException $e) {
$guzzleResponse = $e->getResponse();
$body = $guzzleResponse->getBody()->getContents();
$status = $e->getResponse()->getStatusCode();

try {
$body = $e->getResponse()->getBody()->getContents();
$data = json_decode($body, true, flags: JSON_THROW_ON_ERROR);
if (!is_array($data)) {
throw new \RuntimeException('JSON response is not an array');
Expand All @@ -132,7 +133,15 @@ protected function request(

$clientException = new RemoteClientException($e->getMessage(), $status, $e, $data);
$this->logger->debug('Client error from remote', ['exception' => $clientException]);
return new Response($e->getResponse(), false);

/** @psalm-suppress UndefinedClass,InvalidArgument */
return new Response(new \GuzzleHttp\Psr7\Response(
$guzzleResponse->getStatusCode(),
$guzzleResponse->getHeaders(),
$body,
$guzzleResponse->getProtocolVersion(),
$guzzleResponse->getReasonPhrase(),
), false);
} catch (ServerException|\Throwable $e) {
$serverException = new CannotReachRemoteException($e->getMessage(), $e->getCode(), $e);
$this->logger->error('Could not reach remote', ['exception' => $serverException]);
Expand Down

0 comments on commit 1c7b81f

Please sign in to comment.