From 753191d0331c6d456f715068c62216de5038f64b Mon Sep 17 00:00:00 2001 From: Andrew Menino-Barlow Date: Sat, 27 Apr 2024 15:03:56 +0100 Subject: [PATCH] fix: Improve error handling in OpenFeatureClient The commit adds the flagKey to EvaluationDetailsBuilder during error handling, providing more context for debugging. It also includes error message within the ResolutionError for more complete error information. Signed-off-by: Andrew Menino-Barlow --- src/OpenFeatureClient.php | 3 ++- tests/unit/OpenFeatureClientTest.php | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/OpenFeatureClient.php b/src/OpenFeatureClient.php index 4181a60..0f90aab 100644 --- a/src/OpenFeatureClient.php +++ b/src/OpenFeatureClient.php @@ -372,9 +372,10 @@ private function evaluateFlag( ), ); - $error = $err instanceof ThrowableWithResolutionError ? $err->getResolutionError() : new ResolutionError(ErrorCode::GENERAL()); + $error = $err instanceof ThrowableWithResolutionError ? $err->getResolutionError() : new ResolutionError(ErrorCode::GENERAL(), $err->getMessage()); $details = (new EvaluationDetailsBuilder()) + ->withFlagKey($flagKey) ->withValue($defaultValue) ->withReason(Reason::ERROR) ->withError($error) diff --git a/tests/unit/OpenFeatureClientTest.php b/tests/unit/OpenFeatureClientTest.php index 1b5cd46..55627a8 100644 --- a/tests/unit/OpenFeatureClientTest.php +++ b/tests/unit/OpenFeatureClientTest.php @@ -499,6 +499,7 @@ public function testClientEvaluationDetailsAbnormalExecutionHasErrorCodeField(): $resolutionError = $actualDetails->getError(); $this->assertNotNull($resolutionError); $this->assertEquals($expectedErrorCode, $resolutionError->getResolutionErrorCode()); + $this->assertEquals('flagKey', $actualDetails->getFlagKey()); } /** @@ -563,9 +564,14 @@ public function testClientShouldLogInformativeErrorDuringAbnormalExecution(): vo $client = new OpenFeatureClient($api, 'test-name', 'test-version'); $client->setLogger($mockLogger); - $value = $client->getBooleanValue('flagKey', false); + $details = $client->getBooleanDetails('flagKey', false); - $this->assertEquals($value, false); + $this->assertEquals(false, $details->getValue()); + $this->assertEquals('flagKey', $details->getFlagKey()); + + $this->assertInstanceOf(ResolutionError::class, $details->getError()); + $this->assertEquals(ErrorCode::GENERAL(), $details->getError()->getResolutionErrorCode()); + $this->assertEquals('NETWORK_ERROR', $details->getError()->getResolutionErrorMessage()); } /**