From fb47f031e09fe6ba982b460a9b28ace0d4a44415 Mon Sep 17 00:00:00 2001 From: Thomas Hanke <18329992+y4roc@users.noreply.github.com> Date: Fri, 12 Jul 2024 12:31:35 +0200 Subject: [PATCH 1/6] Add HandlerMessageBody to SentryException --- src/EventListener/MessengerListener.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/EventListener/MessengerListener.php b/src/EventListener/MessengerListener.php index b66b962c..9dd5cb77 100644 --- a/src/EventListener/MessengerListener.php +++ b/src/EventListener/MessengerListener.php @@ -59,6 +59,18 @@ public function handleWorkerMessageFailedEvent(WorkerMessageFailedEvent $event): $scope->setTag('messenger.receiver_name', $event->getReceiverName()); $scope->setTag('messenger.message_class', \get_class($envelope->getMessage())); + $messageBody = json_decode( + json_encode($envelope->getMessage(), JSON_THROW_ON_ERROR), + true, + 512, + JSON_THROW_ON_ERROR + ); + + $scope->addContext('messenger.body', [ + 'title' => 'CommandBus Message Body', + 'body' => $messageBody + ]); + /** @var BusNameStamp|null $messageBusStamp */ $messageBusStamp = $envelope->last(BusNameStamp::class); From cfffa36bf3e873b47e601de6a08669c7597cffb9 Mon Sep 17 00:00:00 2001 From: Thomas Hanke <18329992+y4roc@users.noreply.github.com> Date: Fri, 12 Jul 2024 13:31:00 +0200 Subject: [PATCH 2/6] Add option and error handling --- src/EventListener/MessengerListener.php | 38 ++++++++++++++++++------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/EventListener/MessengerListener.php b/src/EventListener/MessengerListener.php index 9dd5cb77..ba56fadf 100644 --- a/src/EventListener/MessengerListener.php +++ b/src/EventListener/MessengerListener.php @@ -29,16 +29,22 @@ final class MessengerListener */ private $captureSoftFails; + /** + * @var bool Add body of a message to the SentryException + */ + private $captureMessageBody; + /** * @param HubInterface $hub The current hub * @param bool $captureSoftFails Whether to capture errors thrown * while processing a message that * will be retried */ - public function __construct(HubInterface $hub, bool $captureSoftFails = true) + public function __construct(HubInterface $hub, bool $captureSoftFails = true, bool $captureMessageBody = true) { $this->hub = $hub; $this->captureSoftFails = $captureSoftFails; + $this->captureMessageBody = $captureMessageBody; } /** @@ -59,16 +65,9 @@ public function handleWorkerMessageFailedEvent(WorkerMessageFailedEvent $event): $scope->setTag('messenger.receiver_name', $event->getReceiverName()); $scope->setTag('messenger.message_class', \get_class($envelope->getMessage())); - $messageBody = json_decode( - json_encode($envelope->getMessage(), JSON_THROW_ON_ERROR), - true, - 512, - JSON_THROW_ON_ERROR - ); - - $scope->addContext('messenger.body', [ + $scope->setContext('messenger.body', [ 'title' => 'CommandBus Message Body', - 'body' => $messageBody + 'body' => $this->castMessage($envelope->getMessage()); ]); /** @var BusNameStamp|null $messageBusStamp */ @@ -84,6 +83,25 @@ public function handleWorkerMessageFailedEvent(WorkerMessageFailedEvent $event): $this->flushClient(); } + private function castMessage(object $message): mixed + { + if(!$this->captureMessageBody) { + return 'Capture message body is deactivated!'; + } + + try { + return json_decode( + json_encode($message, JSON_THROW_ON_ERROR), + true, + 512, + JSON_THROW_ON_ERROR + ); + + } catch (\JsonException) { + return 'Error while parsing message body.'; + } + } + /** * This method is called for each handled message. * From 9b7c92dd4de60bc367d7e903dd7cc8f97fc05cf3 Mon Sep 17 00:00:00 2001 From: Thomas Hanke <18329992+y4roc@users.noreply.github.com> Date: Fri, 12 Jul 2024 13:34:48 +0200 Subject: [PATCH 3/6] add property description --- src/EventListener/MessengerListener.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/EventListener/MessengerListener.php b/src/EventListener/MessengerListener.php index ba56fadf..baa4e8cc 100644 --- a/src/EventListener/MessengerListener.php +++ b/src/EventListener/MessengerListener.php @@ -35,10 +35,12 @@ final class MessengerListener private $captureMessageBody; /** - * @param HubInterface $hub The current hub - * @param bool $captureSoftFails Whether to capture errors thrown - * while processing a message that - * will be retried + * @param HubInterface $hub The current hub + * @param bool $captureSoftFails Whether to capture errors thrown + * while processing a message that + * will be retried + * @param bool $captureMessageBody Add body of the message to the + * SentryException */ public function __construct(HubInterface $hub, bool $captureSoftFails = true, bool $captureMessageBody = true) { From 5c77f24892b9412bedd468afb72406b9e9907993 Mon Sep 17 00:00:00 2001 From: Thomas Hanke <18329992+y4roc@users.noreply.github.com> Date: Fri, 12 Jul 2024 14:20:36 +0200 Subject: [PATCH 4/6] fixing pslam & phpstan --- src/EventListener/MessengerListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventListener/MessengerListener.php b/src/EventListener/MessengerListener.php index baa4e8cc..80f8752d 100644 --- a/src/EventListener/MessengerListener.php +++ b/src/EventListener/MessengerListener.php @@ -69,7 +69,7 @@ public function handleWorkerMessageFailedEvent(WorkerMessageFailedEvent $event): $scope->setContext('messenger.body', [ 'title' => 'CommandBus Message Body', - 'body' => $this->castMessage($envelope->getMessage()); + 'body' => $this->castMessage($envelope->getMessage()) ]); /** @var BusNameStamp|null $messageBusStamp */ From be973559499959c1b837a5f346bb0571e0e2d9fd Mon Sep 17 00:00:00 2001 From: Thomas Hanke <18329992+y4roc@users.noreply.github.com> Date: Fri, 12 Jul 2024 19:34:24 +0200 Subject: [PATCH 5/6] cs-fixes --- src/EventListener/MessengerListener.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/EventListener/MessengerListener.php b/src/EventListener/MessengerListener.php index 80f8752d..0dfe9a8c 100644 --- a/src/EventListener/MessengerListener.php +++ b/src/EventListener/MessengerListener.php @@ -39,7 +39,7 @@ final class MessengerListener * @param bool $captureSoftFails Whether to capture errors thrown * while processing a message that * will be retried - * @param bool $captureMessageBody Add body of the message to the + * @param bool $captureMessageBody Add body of the message to the * SentryException */ public function __construct(HubInterface $hub, bool $captureSoftFails = true, bool $captureMessageBody = true) @@ -69,7 +69,7 @@ public function handleWorkerMessageFailedEvent(WorkerMessageFailedEvent $event): $scope->setContext('messenger.body', [ 'title' => 'CommandBus Message Body', - 'body' => $this->castMessage($envelope->getMessage()) + 'body' => $this->castMessage($envelope->getMessage()), ]); /** @var BusNameStamp|null $messageBusStamp */ @@ -87,20 +87,19 @@ public function handleWorkerMessageFailedEvent(WorkerMessageFailedEvent $event): private function castMessage(object $message): mixed { - if(!$this->captureMessageBody) { + if (!$this->captureMessageBody) { return 'Capture message body is deactivated!'; } - + try { return json_decode( - json_encode($message, JSON_THROW_ON_ERROR), + json_encode($message, \JSON_THROW_ON_ERROR), true, 512, - JSON_THROW_ON_ERROR + \JSON_THROW_ON_ERROR ); - } catch (\JsonException) { - return 'Error while parsing message body.'; + return 'Error while parsing message body.'; } } From 3cf54814d0fcc98ebef50f3f4098efa04621a8b0 Mon Sep 17 00:00:00 2001 From: Thomas Hanke <18329992+y4roc@users.noreply.github.com> Date: Fri, 12 Jul 2024 20:55:42 +0200 Subject: [PATCH 6/6] fixing pipeline --- src/EventListener/MessengerListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventListener/MessengerListener.php b/src/EventListener/MessengerListener.php index 0dfe9a8c..3ee1cf8d 100644 --- a/src/EventListener/MessengerListener.php +++ b/src/EventListener/MessengerListener.php @@ -98,7 +98,7 @@ private function castMessage(object $message): mixed 512, \JSON_THROW_ON_ERROR ); - } catch (\JsonException) { + } catch (\JsonException $e) { return 'Error while parsing message body.'; } }