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: 2 additions & 0 deletions src/LaunchDarkly/Impl/UnrecoverableHTTPStatusException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class UnrecoverableHTTPStatusException extends \Exception

public function __construct(int $status)
{
// Do not chain the Guzzle exception. Its request has the SDK key.
parent::__construct("HTTP error $status", $status);
$this->status = $status;
}
}
29 changes: 29 additions & 0 deletions tests/LDClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace LaunchDarkly\Tests;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use InvalidArgumentException;
use LaunchDarkly\BigSegmentsEvaluationStatus;
use LaunchDarkly\EvaluationDetail;
use LaunchDarkly\EvaluationReason;
use LaunchDarkly\Impl\Integrations\GuzzleFeatureRequester;
use LaunchDarkly\Impl\Model\FeatureFlag;
use LaunchDarkly\Impl\UnrecoverableHTTPStatusException;
use LaunchDarkly\LDClient;
Expand All @@ -20,6 +24,7 @@
use LaunchDarkly\Types\BigSegmentsStoreMetadata;
use LaunchDarkly\Types\BigSegmentsStoreStatus;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class LDClientTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -153,6 +158,30 @@ public function testUnrecoverableHTTPErrorStillTakesClientOffline(int $status):
$this->assertNull($second->getReason()->getException());
}

/** @dataProvider unrecoverableHTTPStatuses */
public function testVariationDetailExposesHTTPStatusInException(int $status): void
{
$requester = new GuzzleFeatureRequester('http://example.invalid', 'sdk-key', [
'logger' => new NullLogger(),
'timeout' => 3,
'connect_timeout' => 3,
]);
$mock = new MockHandler([new Response($status)]);
$httpClient = new \GuzzleHttp\Client(['handler' => HandlerStack::create($mock)]);
(new \ReflectionProperty($requester, '_client'))->setValue($requester, $httpClient);
$client = $this->makeClient(['feature_requester' => $requester]);

$detail = $client->variationDetail('feature', LDContext::create('userkey'), 'default');
$exception = $detail->getReason()->getException();

$this->assertInstanceOf(UnrecoverableHTTPStatusException::class, $exception);
$this->assertSame("HTTP error $status", $exception->getMessage());
$this->assertSame($status, $exception->getCode());
$this->assertSame($status, $exception->status);
// The Guzzle exception holds the request and its Authorization header.
$this->assertNull($exception->getPrevious());
}

public function testVariationReturnsDefaultIfFlagEvaluatesToNull()
{
$flag = $this->makeFlagThatEvaluatesToNull('feature');
Expand Down
Loading