This commit is contained in:
2026-11-27 17:34:16 +08:00
parent 2aa9e5097f
commit e0e860f330
1111 changed files with 153644 additions and 7 deletions
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
/**
* Exception thrown for HTTP responses with 4xx or 5xx status codes.
*/
class BadResponseException extends ResponseException
{
}
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
/**
* Exception thrown for HTTP responses with 4xx status codes.
*/
class ClientException extends BadResponseException
{
}
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
/**
* Exception thrown when connection establishment fails.
*/
class ConnectException extends NetworkException
{
}
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
/**
* Exception thrown when connection establishment exceeds the time limit.
*/
class ConnectTimeoutException extends ConnectException
{
}
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
use Psr\Http\Client\ClientExceptionInterface;
interface GuzzleException extends ClientExceptionInterface
{
}
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
/**
* Exception thrown when a handler is closed before a transfer completes.
*/
class HandlerClosedException extends TransferException
{
}
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
/**
* Exception thrown when an invalid argument is supplied to Guzzle.
*/
final class InvalidArgumentException extends \InvalidArgumentException implements GuzzleException
{
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Message\RequestInterface;
/**
* Base exception for transfer failures without a response.
*/
class NetworkException extends TransferException implements NetworkExceptionInterface
{
public function __construct(
string $message,
RequestInterface $request,
?\Throwable $previous = null
) {
parent::__construct($message, $request, 0, $previous);
}
}
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
/**
* Exception thrown when a transfer times out before response headers are
* received.
*/
class NetworkTimeoutException extends NetworkException
{
}
@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
use GuzzleHttp\BodySummarizer;
use GuzzleHttp\BodySummarizerInterface;
use GuzzleHttp\Psr7\DiagnosticValue;
use Psr\Http\Client\RequestExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Base exception for request failures associated with a request.
*/
class RequestException extends TransferException implements RequestExceptionInterface
{
public function __construct(
string $message,
RequestInterface $request,
int $code = 0,
?\Throwable $previous = null
) {
parent::__construct($message, $request, $code, $previous);
}
/**
* Factory method to create a new exception with a normalized error message
*
* @param RequestInterface $request Request sent
* @param ResponseInterface|null $response Response received, if any
* @param \Throwable|null $previous Previous exception
* @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer
*/
public static function create(
#[\SensitiveParameter]
RequestInterface $request,
#[\SensitiveParameter]
?ResponseInterface $response = null,
#[\SensitiveParameter]
?\Throwable $previous = null,
?BodySummarizerInterface $bodySummarizer = null
): self {
if (!$response) {
return new self(
'Error completing request',
$request,
0,
$previous
);
}
$level = (int) \floor($response->getStatusCode() / 100);
if ($level === 4) {
$label = 'Client error';
} elseif ($level === 5) {
$label = 'Server error';
} else {
$label = 'Unsuccessful request';
}
$uri = \GuzzleHttp\Psr7\UriRedactor::redactUriForMessage($request->getUri());
// Client error: `GET /` resulted in a `404 Not Found` response: <html> ... (truncated)
$message = \sprintf(
'%s: `%s %s` resulted in a `%s %s` response',
$label,
DiagnosticValue::escape($request->getMethod()),
$uri,
$response->getStatusCode(),
DiagnosticValue::escape($response->getReasonPhrase())
);
$summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response);
if ($summary !== null) {
$message .= \sprintf(': %s', DiagnosticValue::escape($summary));
}
if ($level === 4) {
return new ClientException($message, $request, $response, $previous);
}
if ($level === 5) {
return new ServerException($message, $request, $response, $previous);
}
return new ResponseException($message, $request, $response, $previous);
}
}
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Exception thrown when a request fails after response headers are received.
*/
class ResponseException extends RequestException
{
private ResponseInterface $response;
final public function __construct(
string $message,
#[\SensitiveParameter]
RequestInterface $request,
#[\SensitiveParameter]
ResponseInterface $response,
#[\SensitiveParameter]
?\Throwable $previous = null
) {
parent::__construct($message, $request, $response->getStatusCode(), $previous);
$this->response = $response;
}
/**
* Return a new exception instance with an updated response.
*
* @param \Throwable|null $previous Previous exception, or the current
* previous exception when omitted.
*
* @return static
*/
public function withResponse(
#[\SensitiveParameter]
ResponseInterface $response,
#[\SensitiveParameter]
?\Throwable $previous = null
): self {
if ($response->getStatusCode() !== $this->response->getStatusCode()) {
throw new InvalidArgumentException('Cannot replace response with a different status code.');
}
return new static(
$this->getMessage(),
$this->getRequest(),
$response,
$previous ?? $this->getPrevious()
);
}
/**
* Get the associated response.
*/
public function getResponse(): ResponseInterface
{
return $this->response;
}
}
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
/**
* Exception thrown when a transfer times out after response headers are
* received.
*/
class ResponseTimeoutException extends ResponseTransferException
{
}
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
/**
* Exception thrown when a response transfer fails after response headers are
* received.
*/
class ResponseTransferException extends ResponseException
{
}
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
/**
* Exception thrown for HTTP responses with 5xx status codes.
*/
class ServerException extends BadResponseException
{
}
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
/**
* Exception thrown when redirect middleware reaches the redirect limit.
*/
class TooManyRedirectsException extends ResponseException
{
}
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Exception;
use Psr\Http\Message\RequestInterface;
/**
* Base exception for transfer failures.
*/
class TransferException extends \RuntimeException implements GuzzleException
{
private RequestInterface $request;
public function __construct(
string $message,
RequestInterface $request,
int $code = 0,
?\Throwable $previous = null
) {
parent::__construct($message, $code, $previous);
$this->request = $request;
}
/**
* Get the request that caused the exception.
*/
public function getRequest(): RequestInterface
{
return $this->request;
}
}