Skip to content

Commit 7ebe1ca

Browse files
authored
prepare 3.2.0 release (#106)
1 parent 3eee7c9 commit 7ebe1ca

File tree

6 files changed

+59
-23
lines changed

6 files changed

+59
-23
lines changed

src/LaunchDarkly/GuzzleEventPublisher.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ public function publish($payload)
6060
$this->_logger->warning("GuzzleEventPublisher::publish caught $e");
6161
return false;
6262
}
63-
if ($response && ($response->getStatusCode() == 401)) {
64-
throw new InvalidSDKKeyException();
63+
if ($response && ($response->getStatusCode() >= 300)) {
64+
$this->_logger->error(Util::httpErrorMessage($response->getStatusCode(), 'event posting', 'some events were dropped'));
65+
if (!Util::isHttpErrorRecoverable($response->getStatusCode())) {
66+
throw new UnrecoverableHTTPStatusException($response->getStatusCode());
67+
}
68+
return false;
6569
}
66-
return $response && ($response->getStatusCode() < 300);
70+
return $response != null;
6771
}
6872
}

src/LaunchDarkly/GuzzleFeatureRequester.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ public function getAllFeatures()
115115

116116
private function handleUnexpectedStatus($code, $method)
117117
{
118-
$this->_logger->error("$method received an unexpected HTTP status code $code");
119-
if ($code == 401) {
120-
throw new InvalidSDKKeyException();
118+
$this->_logger->error(Util::httpErrorMessage($code, $method, 'default value was returned'));
119+
if (!Util::isHttpErrorRecoverable($code)) {
120+
throw new UnrecoverableHTTPStatusException($code);
121121
}
122122
}
123123
}

src/LaunchDarkly/InvalidSDKKeyException.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/LaunchDarkly/LDClient.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ public function variation($key, $user, $default = false)
146146
}
147147
try {
148148
$flag = $this->_featureRequester->getFeature($key);
149-
} catch (InvalidSDKKeyException $e) {
150-
$this->handleInvalidSDKKey();
149+
} catch (UnrecoverableHTTPStatusException $e) {
150+
$this->handleUnrecoverableError();
151151
return $default;
152152
}
153153

@@ -275,8 +275,8 @@ public function allFlags($user)
275275
}
276276
try {
277277
$flags = $this->_featureRequester->getAllFeatures();
278-
} catch (InvalidSDKKeyException $e) {
279-
$this->handleInvalidSDKKey();
278+
} catch (UnrecoverableHTTPStatusException $e) {
279+
$this->handleUnrecoverableError();
280280
return null;
281281
}
282282
if ($flags === null) {
@@ -314,8 +314,8 @@ public function flush()
314314
{
315315
try {
316316
return $this->_eventProcessor->flush();
317-
} catch (InvalidSDKKeyException $e) {
318-
$this->handleInvalidSDKKey();
317+
} catch (UnrecoverableHTTPStatusException $e) {
318+
$this->handleUnrecoverableError();
319319
}
320320
}
321321

@@ -345,9 +345,9 @@ protected function _get_default($key, $default)
345345
}
346346
}
347347

348-
protected function handleInvalidSDKKey()
348+
protected function handleUnrecoverableError()
349349
{
350-
$this->_logger->error("Received 401 error, no further HTTP requests will be made during lifetime of LDClient since SDK key is invalid");
350+
$this->_logger->error("Due to an unrecoverable HTTP error, no further HTTP requests will be made during lifetime of LDClient");
351351
$this->_offline = true;
352352
}
353353
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace LaunchDarkly;
3+
4+
/**
5+
* Used internally.
6+
*/
7+
class UnrecoverableHTTPStatusException extends \Exception
8+
{
9+
public $status;
10+
11+
public function __construct($status)
12+
{
13+
$this->status = $status;
14+
}
15+
}

src/LaunchDarkly/Util.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,30 @@ public static function newFeatureRequestEvent($key, $user, $variation, $value, $
5151
$event['prereqOf'] = $prereqOf;
5252
return $event;
5353
}
54+
55+
/**
56+
* @param $status int
57+
* @return boolean
58+
*/
59+
public static function isHttpErrorRecoverable($status)
60+
{
61+
if ($status >= 400 && $status < 500) {
62+
return ($status == 400) || ($status == 408) || ($status == 429);
63+
}
64+
return true;
65+
}
66+
67+
/**
68+
* @param $status int
69+
* @param $context string
70+
* @param $retryMessage string
71+
* @return string
72+
*/
73+
public static function httpErrorMessage($status, $context, $retryMessage)
74+
{
75+
return 'Received error ' . $status
76+
. (($status == 401) ? ' (invalid SDK key)' : '')
77+
. ' for ' . $context . ' - '
78+
. (Util::isHttpErrorRecoverable($status) ? $retryMessage : 'giving up permanently');
79+
}
5480
}

0 commit comments

Comments
 (0)