-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs AB#7245 Add error logs for console commands and add ability to s…
…end the X-Context-ID header on a response (#28)
- Loading branch information
Showing
10 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AnzuSystems\CommonBundle\Event\Listener; | ||
|
||
use AnzuSystems\CommonBundle\Log\Factory\LogContextFactory; | ||
use AnzuSystems\SerializerBundle\Exception\SerializerException; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Event\ConsoleErrorEvent; | ||
|
||
final class ConsoleExceptionListener | ||
{ | ||
public function __construct( | ||
private readonly LoggerInterface $appLogger, | ||
private readonly array $ignoredExceptions = [], | ||
private readonly ?LogContextFactory $logContextFactory = null, | ||
) { | ||
} | ||
|
||
/** | ||
* @throws SerializerException | ||
*/ | ||
public function __invoke(ConsoleErrorEvent $event): void | ||
{ | ||
$exception = $event->getError(); | ||
if (in_array($exception::class, $this->ignoredExceptions, true)) { | ||
return; | ||
} | ||
|
||
$context = []; | ||
if ($this->logContextFactory instanceof LogContextFactory) { | ||
$context = $this->logContextFactory->buildFromConsoleErrorEventToArray($event); | ||
} | ||
|
||
$this->appLogger->critical(sprintf( | ||
'[Command] [%s] %s', | ||
(string) $event->getCommand()?->getName(), | ||
$exception->getMessage() | ||
), $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AnzuSystems\CommonBundle\Event\Listener; | ||
|
||
use AnzuSystems\CommonBundle\Kernel\AnzuKernel; | ||
use AnzuSystems\Contracts\AnzuApp; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
|
||
final class ContextIdOnResponseListener | ||
{ | ||
public function __invoke(ResponseEvent $event): void | ||
{ | ||
$response = $event->getResponse(); | ||
if ($response->headers->has(AnzuKernel::CONTEXT_IDENTITY_HEADER)) { | ||
return; | ||
} | ||
|
||
$response->headers->set(AnzuKernel::CONTEXT_IDENTITY_HEADER, AnzuApp::getContextId()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AnzuSystems\CommonBundle\Tests\Controller; | ||
|
||
use AnzuSystems\CommonBundle\Kernel\AnzuKernel; | ||
use AnzuSystems\Contracts\AnzuApp; | ||
use JsonException; | ||
|
||
final class ContentIdOnResponseTest extends AbstractControllerTest | ||
{ | ||
/** | ||
* @throws JsonException | ||
*/ | ||
public function testContextId(): void | ||
{ | ||
$this->get(uri: '/something'); | ||
|
||
$this->assertTrue(self::$client->getResponse()->headers->has(AnzuKernel::CONTEXT_IDENTITY_HEADER)); | ||
$this->assertSame( | ||
expected: AnzuApp::getContextId(), | ||
actual: self::$client->getResponse()->headers->get(AnzuKernel::CONTEXT_IDENTITY_HEADER), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters