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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^8.2",
"ext-curl": "^8.2",
"guzzlehttp/guzzle": "^7.0",
"guzzlehttp/guzzle": "^7.0 || ^8.0",
Comment thread
VincentLanglet marked this conversation as resolved.
"paragonie/halite": "^5.1"
},
"require-dev": {
Expand Down
36 changes: 25 additions & 11 deletions lib/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ConnectTimeoutException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ResponseException;
use Psr\Http\Message\ResponseInterface;
use WorkOS\Exception\ApiException;
use WorkOS\Exception\AuthenticationException;
Expand Down Expand Up @@ -120,17 +122,22 @@ public function request(

throw $this->mapTransportException($e);
} catch (RequestException $e) {
if ($e->hasResponse()) {
$response = null;
if ($e instanceof ResponseException) {
$response = $e->getResponse();
if ($response !== null) {
$statusCode = $response->getStatusCode();
if (in_array($statusCode, self::RETRY_STATUS_CODES, true) && $attempt < $maxRetries) {
$this->sleep($attempt, $response->getHeaderLine('Retry-After'));
continue;
}

throw $this->mapApiException($response, $e);
} elseif (method_exists($e, 'getResponse')) {
// BC layer for guzzle 7
$response = $e->getResponse();
}

if ($response instanceof ResponseInterface) {
$statusCode = $response->getStatusCode();
if (in_array($statusCode, self::RETRY_STATUS_CODES, true) && $attempt < $maxRetries) {
$this->sleep($attempt, $response->getHeaderLine('Retry-After'));
continue;
}

throw $this->mapApiException($response, $e);
}

if ($attempt < $maxRetries) {
Expand Down Expand Up @@ -417,10 +424,17 @@ private function mapTransportException(\Throwable $exception): \Exception
private function isTimeoutException(\Throwable $exception): bool
{
if ($exception instanceof ConnectException || $exception instanceof RequestException) {
$errno = $exception->getHandlerContext()['errno'] ?? null;
if ($errno === 28) {
if ($exception instanceof ConnectTimeoutException) {
return true;
}

if (method_exists($exception, 'getHandlerContext')) {
// BC layer for guzzle 7
$errno = $exception->getHandlerContext()['errno'] ?? null;
if ($errno === 28) {
return true;
}
}
}

return str_contains(strtolower($exception->getMessage()), 'timed out');
Expand Down
Loading