From e37331dc8f8ef2747ec33a83d897a74e574914e9 Mon Sep 17 00:00:00 2001 From: sinansoezen Date: Tue, 31 Aug 2021 09:56:25 +0200 Subject: [PATCH 1/3] check if session exists before accessing it --- .../Processor/ExceptionEnricherProcessor.php | 2 +- .../Processor/ExceptionEnricherProcessorTest.php | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/ExceptionEnricher/Processor/ExceptionEnricherProcessor.php b/src/ExceptionEnricher/Processor/ExceptionEnricherProcessor.php index 8002c53..6ff6c04 100644 --- a/src/ExceptionEnricher/Processor/ExceptionEnricherProcessor.php +++ b/src/ExceptionEnricher/Processor/ExceptionEnricherProcessor.php @@ -45,7 +45,7 @@ public function __invoke(array $record): array $record['extra']['request_ip'] = $this->requestStack->getMainRequest()->getClientIp(); } - if ($this->requestStack->getSession() && $this->requestStack->getSession()->getId()) { + if ($this->requestStack->getCurrentRequest()->hasSession() && $this->requestStack->getSession()->getId()) { $record['extra']['session_id'] = $this->requestStack->getSession()->getId(); } } diff --git a/tests/ExceptionEnricher/Processor/ExceptionEnricherProcessorTest.php b/tests/ExceptionEnricher/Processor/ExceptionEnricherProcessorTest.php index 8d01e06..ffa115e 100644 --- a/tests/ExceptionEnricher/Processor/ExceptionEnricherProcessorTest.php +++ b/tests/ExceptionEnricher/Processor/ExceptionEnricherProcessorTest.php @@ -43,6 +43,7 @@ public function testFullProcessor() $request->getClientIp()->willReturn('127.0.0.1')->shouldBeCalled(); $request->getMethod()->willReturn('GET')->shouldBeCalled(); $request->getRequestUri()->willReturn('/testroute/')->shouldBeCalled(); + $request->hasSession()->willReturn(true)->shouldBeCalled(); $session = $this->prophesize(SessionInterface::class); $session->getId()->willReturn('39d9f31fb12441428031e26d2f83ab6e')->shouldBeCalled(); @@ -75,12 +76,12 @@ public function testFullProcessor() $this->assertSame('39d9f31fb12441428031e26d2f83ab6e', $record['extra']['session_id']); } - private function createRecord($level = Logger::ERROR, $message = 'An Exception has been encountered.'): array + private function createRecord(): array { return [ - 'message' => $message, - 'level' => $level, - 'level_name' => Logger::getLevelName($level), + 'message' => 'An Exception has been encountered.', + 'level' => Logger::ERROR, + 'level_name' => Logger::getLevelName(Logger::ERROR), 'channel' => 'test', 'datetime' => new DateTimeImmutable('now'), 'extra' => [], From bb2e399ca77e88e4636bfb70b1629db3cb688dc3 Mon Sep 17 00:00:00 2001 From: sinansoezen Date: Tue, 31 Aug 2021 09:59:14 +0200 Subject: [PATCH 2/3] added return value --- src/ExceptionEnricher/ExceptionEnricherBundle.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ExceptionEnricher/ExceptionEnricherBundle.php b/src/ExceptionEnricher/ExceptionEnricherBundle.php index a8ed7e9..6f889ea 100644 --- a/src/ExceptionEnricher/ExceptionEnricherBundle.php +++ b/src/ExceptionEnricher/ExceptionEnricherBundle.php @@ -9,7 +9,7 @@ class ExceptionEnricherBundle extends Bundle { - public function getContainerExtension() + public function getContainerExtension(): ExceptionEnricherExtension { return new ExceptionEnricherExtension(); } From 0542b207d34517f11e9afc6deeac98f809db758b Mon Sep 17 00:00:00 2001 From: sinansoezen Date: Tue, 31 Aug 2021 10:10:20 +0200 Subject: [PATCH 3/3] this is starting to get annoying --- .../Processor/ExceptionEnricherProcessor.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ExceptionEnricher/Processor/ExceptionEnricherProcessor.php b/src/ExceptionEnricher/Processor/ExceptionEnricherProcessor.php index 6ff6c04..681a9b2 100644 --- a/src/ExceptionEnricher/Processor/ExceptionEnricherProcessor.php +++ b/src/ExceptionEnricher/Processor/ExceptionEnricherProcessor.php @@ -39,15 +39,15 @@ public function __invoke(array $record): array if ($this->requestStack->getCurrentRequest()->headers) { $record['extra']['request_user_agent'] = $this->requestStack->getCurrentRequest()->headers->get('User-Agent'); } + + if ($this->requestStack->getCurrentRequest()->hasSession() && $this->requestStack->getSession()->getId()) { + $record['extra']['session_id'] = $this->requestStack->getSession()->getId(); + } } if ($this->requestStack->getMainRequest()) { $record['extra']['request_ip'] = $this->requestStack->getMainRequest()->getClientIp(); } - - if ($this->requestStack->getCurrentRequest()->hasSession() && $this->requestStack->getSession()->getId()) { - $record['extra']['session_id'] = $this->requestStack->getSession()->getId(); - } } if ($this->tokenStorage && $this->tokenStorage->getToken()) {