Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Require custom implementations and subclasses of public command APIs to match native method signatures
* Require service client response transformers to return `ResultInterface` values
* Require custom middleware, transformers, and `executeAll()` callbacks to accept exact argument types instead of relying on scalar coercion
* Preserve requests from PSR-18 request and network exceptions when wrapping command failures

## 1.4.0 - 2026-05-18

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"php": "^7.4 || ^8.0",
"guzzlehttp/guzzle": "^8.0@dev",
"guzzlehttp/promises": "^3.0@dev",
"guzzlehttp/psr7": "^3.0@dev"
"guzzlehttp/psr7": "^3.0@dev",
"psr/http-client": "^1.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.2",
Expand Down
10 changes: 8 additions & 2 deletions src/Exception/CommandException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Client\RequestExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -28,10 +30,14 @@ public static function fromPrevious(CommandInterface $command, \Exception $prev)
return $prev;
}

// If the exception is a RequestException, get the Request and Response.
// If the exception is request-aware, preserve the Request.
$request = $response = null;
if ($prev instanceof RequestException) {
if ($prev instanceof RequestExceptionInterface || $prev instanceof NetworkExceptionInterface) {
$request = $prev->getRequest();
}

// Guzzle RequestException also exposes the optional Response.
if ($prev instanceof RequestException) {
$response = $prev->getResponse();
}

Expand Down
39 changes: 39 additions & 0 deletions tests/Exception/CommandExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use GuzzleHttp\Command\Exception\CommandClientException;
use GuzzleHttp\Command\Exception\CommandException;
use GuzzleHttp\Command\Exception\CommandServerException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\RequestExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -62,4 +64,41 @@ public function testFactoryReturnsServerExceptionFor500LevelStatusCode(): void
$exception = CommandException::fromPrevious($command, $previous);
$this->assertInstanceOf(CommandServerException::class, $exception);
}

public function testFactoryCopiesRequestFromNetworkException(): void
{
$command = $this->createMock(CommandInterface::class);
$request = $this->createMock(RequestInterface::class);
$previous = new ConnectException('error', $request);

$exception = CommandException::fromPrevious($command, $previous);
$this->assertSame($request, $exception->getRequest());
$this->assertNull($exception->getResponse());
$this->assertSame($previous, $exception->getPrevious());
}

public function testFactoryCopiesRequestFromPsrRequestException(): void
{
$command = $this->createMock(CommandInterface::class);
$request = $this->createMock(RequestInterface::class);
$previous = new class('error', $request) extends \RuntimeException implements RequestExceptionInterface {
private RequestInterface $request;

public function __construct(string $message, RequestInterface $request)
{
parent::__construct($message);
$this->request = $request;
}

public function getRequest(): RequestInterface
{
return $this->request;
}
};

$exception = CommandException::fromPrevious($command, $previous);
$this->assertSame($request, $exception->getRequest());
$this->assertNull($exception->getResponse());
$this->assertSame($previous, $exception->getPrevious());
}
}