diff --git a/src/LaunchDarkly/GuzzleEventPublisher.php b/src/LaunchDarkly/GuzzleEventPublisher.php index ce18f0f14..7dc458950 100644 --- a/src/LaunchDarkly/GuzzleEventPublisher.php +++ b/src/LaunchDarkly/GuzzleEventPublisher.php @@ -60,9 +60,13 @@ public function publish($payload) $this->_logger->warning("GuzzleEventPublisher::publish caught $e"); return false; } - if ($response && ($response->getStatusCode() == 401)) { - throw new InvalidSDKKeyException(); + if ($response && ($response->getStatusCode() >= 300)) { + $this->_logger->error(Util::httpErrorMessage($response->getStatusCode(), 'event posting', 'some events were dropped')); + if (!Util::isHttpErrorRecoverable($response->getStatusCode())) { + throw new UnrecoverableHTTPStatusException($response->getStatusCode()); + } + return false; } - return $response && ($response->getStatusCode() < 300); + return $response != null; } } diff --git a/src/LaunchDarkly/GuzzleFeatureRequester.php b/src/LaunchDarkly/GuzzleFeatureRequester.php index 65f446d8d..efd40c59c 100644 --- a/src/LaunchDarkly/GuzzleFeatureRequester.php +++ b/src/LaunchDarkly/GuzzleFeatureRequester.php @@ -115,9 +115,9 @@ public function getAllFeatures() private function handleUnexpectedStatus($code, $method) { - $this->_logger->error("$method received an unexpected HTTP status code $code"); - if ($code == 401) { - throw new InvalidSDKKeyException(); + $this->_logger->error(Util::httpErrorMessage($code, $method, 'default value was returned')); + if (!Util::isHttpErrorRecoverable($code)) { + throw new UnrecoverableHTTPStatusException($code); } } } diff --git a/src/LaunchDarkly/InvalidSDKKeyException.php b/src/LaunchDarkly/InvalidSDKKeyException.php deleted file mode 100644 index 7eb7795bb..000000000 --- a/src/LaunchDarkly/InvalidSDKKeyException.php +++ /dev/null @@ -1,9 +0,0 @@ -_featureRequester->getFeature($key); - } catch (InvalidSDKKeyException $e) { - $this->handleInvalidSDKKey(); + } catch (UnrecoverableHTTPStatusException $e) { + $this->handleUnrecoverableError(); return $default; } @@ -275,8 +275,8 @@ public function allFlags($user) } try { $flags = $this->_featureRequester->getAllFeatures(); - } catch (InvalidSDKKeyException $e) { - $this->handleInvalidSDKKey(); + } catch (UnrecoverableHTTPStatusException $e) { + $this->handleUnrecoverableError(); return null; } if ($flags === null) { @@ -314,8 +314,8 @@ public function flush() { try { return $this->_eventProcessor->flush(); - } catch (InvalidSDKKeyException $e) { - $this->handleInvalidSDKKey(); + } catch (UnrecoverableHTTPStatusException $e) { + $this->handleUnrecoverableError(); } } @@ -345,9 +345,9 @@ protected function _get_default($key, $default) } } - protected function handleInvalidSDKKey() + protected function handleUnrecoverableError() { - $this->_logger->error("Received 401 error, no further HTTP requests will be made during lifetime of LDClient since SDK key is invalid"); + $this->_logger->error("Due to an unrecoverable HTTP error, no further HTTP requests will be made during lifetime of LDClient"); $this->_offline = true; } } diff --git a/src/LaunchDarkly/UnrecoverableHTTPStatusException.php b/src/LaunchDarkly/UnrecoverableHTTPStatusException.php new file mode 100644 index 000000000..4e1b5ad44 --- /dev/null +++ b/src/LaunchDarkly/UnrecoverableHTTPStatusException.php @@ -0,0 +1,15 @@ +status = $status; + } +} diff --git a/src/LaunchDarkly/Util.php b/src/LaunchDarkly/Util.php index f25f241e0..1fbf79010 100644 --- a/src/LaunchDarkly/Util.php +++ b/src/LaunchDarkly/Util.php @@ -51,4 +51,30 @@ public static function newFeatureRequestEvent($key, $user, $variation, $value, $ $event['prereqOf'] = $prereqOf; return $event; } + + /** + * @param $status int + * @return boolean + */ + public static function isHttpErrorRecoverable($status) + { + if ($status >= 400 && $status < 500) { + return ($status == 400) || ($status == 408) || ($status == 429); + } + return true; + } + + /** + * @param $status int + * @param $context string + * @param $retryMessage string + * @return string + */ + public static function httpErrorMessage($status, $context, $retryMessage) + { + return 'Received error ' . $status + . (($status == 401) ? ' (invalid SDK key)' : '') + . ' for ' . $context . ' - ' + . (Util::isHttpErrorRecoverable($status) ? $retryMessage : 'giving up permanently'); + } }