Skip to content

Commit d28d9c3

Browse files
authored
feat(log): Improve error log in OpenFeatureClient (#156)
Change log message to follow best practice of PSR3, Log Exception object to help debuging, when provider throw Exception <!-- Please use this template for your pull request. --> <!-- Please use the sections that you need and delete other sections --> ## This PR <!-- add the description of the PR here --> - Improve the log message and context, when the OpenFeatureClient catch Exception ### Related Issues <!-- add here the GitHub issue that this PR resolves if applicable --> ### Notes <!-- any additional notes for this PR --> ### Follow-up Tasks <!-- anything that is related to this PR but not done here should be noted under this section --> <!-- if there is a need for a new issue, please link it here --> ### How to test <!-- if applicable, add testing instructions under this section --> - Unit test updated to test the changes --------- Signed-off-by: jaugustin <[email protected]>
1 parent 2c4dba8 commit d28d9c3

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/OpenFeatureClient.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
use function array_merge;
4040
use function array_reverse;
41-
use function sprintf;
4241

4342
class OpenFeatureClient implements Client, LoggerAwareInterface
4443
{
@@ -365,11 +364,12 @@ private function evaluateFlag(
365364
$hookExecutor->afterHooks($flagValueType, $hookContext, $resolutionDetails, $mergedRemainingHooks, $hookHints);
366365
} catch (Throwable $err) {
367366
$this->getLogger()->error(
368-
sprintf(
369-
"An error occurred during feature flag evaluation of flag '%s': %s",
370-
$flagKey,
371-
$err->getMessage(),
372-
),
367+
"An error occurred during feature flag evaluation of flag '{flagKey}': {errorMessage}",
368+
[
369+
'flagKey' => $flagKey,
370+
'errorMessage' => $err->getMessage(),
371+
'exception' => $err,
372+
],
373373
);
374374

375375
$error = $err instanceof ThrowableWithResolutionError ? $err->getResolutionError() : new ResolutionError(ErrorCode::GENERAL(), $err->getMessage());

tests/unit/OpenFeatureClientTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OpenFeature\interfaces\provider\Provider;
2828
use Psr\Log\LoggerInterface;
2929
use ReflectionClass;
30+
use Throwable;
3031

3132
class OpenFeatureClientTest extends TestCase
3233
{
@@ -556,7 +557,21 @@ public function testClientShouldLogInformativeErrorDuringAbnormalExecution(): vo
556557

557558
/** @var LoggerInterface|MockInterface $mockLogger */
558559
$mockLogger = $this->mockery(LoggerInterface::class);
559-
$mockLogger->shouldReceive('error')->once();
560+
$mockLogger->shouldReceive('error')->with(
561+
"An error occurred during feature flag evaluation of flag '{flagKey}': {errorMessage}",
562+
Mockery::on(function ($context) {
563+
$this->assertIsArray($context);
564+
$this->assertArrayHasKey('flagKey', $context);
565+
$this->assertSame('flagKey', $context['flagKey']);
566+
$this->assertArrayHasKey('errorMessage', $context);
567+
$this->assertSame('NETWORK_ERROR', $context['errorMessage']);
568+
$this->assertArrayHasKey('exception', $context);
569+
$this->assertInstanceOf(Throwable::class, $context['exception']);
570+
$this->assertSame('NETWORK_ERROR', $context['exception']->getMessage());
571+
572+
return true;
573+
}),
574+
)->once();
560575
$api->setLogger($mockLogger);
561576

562577
$mockProvider = $this->getDefaultMockProvider();

0 commit comments

Comments
 (0)