Skip to content

Commit

Permalink
fix: Improve error handling in OpenFeatureClient (#121)
Browse files Browse the repository at this point in the history
## This PR

The PR 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.

### How to test
Modify the NoOpProvider to throw any exception with a message. This
should then propergate through to this new error handling

Signed-off-by: Andrew Menino-Barlow <[email protected]>
  • Loading branch information
schodemeiss authored May 7, 2024
1 parent 2123274 commit 58e97d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/OpenFeatureClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/OpenFeatureClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ public function testClientEvaluationDetailsAbnormalExecutionHasErrorCodeField():
$resolutionError = $actualDetails->getError();
$this->assertNotNull($resolutionError);
$this->assertEquals($expectedErrorCode, $resolutionError->getResolutionErrorCode());
$this->assertEquals('flagKey', $actualDetails->getFlagKey());
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand Down

0 comments on commit 58e97d2

Please sign in to comment.