Created
September 5, 2018 10:18
-
-
Save fritz-gerneth/78faaf4b7569845ea57b8864d0fae53d to your computer and use it in GitHub Desktop.
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
<?php | |
class ErrorLoggerPlugin extends AbstractPlugin | |
{ | |
/** @var Logger */ | |
private $logger; | |
public function __construct(Logger $logger) | |
{ | |
$this->logger = $logger; | |
} | |
public function attachToMessageBus(MessageBus $messageBus): void | |
{ | |
$messageBus->attach( | |
MessageBus::EVENT_FINALIZE, | |
function (ActionEvent $actionEvent) { | |
return $this->checkForExceptions($actionEvent); | |
}, | |
1000 | |
); | |
} | |
private function checkForExceptions(ActionEvent $actionEvent) | |
{ | |
$exception = $actionEvent->getParam(MessageBus::EVENT_PARAM_EXCEPTION); | |
if (!$exception instanceof Throwable) { | |
return; | |
} | |
/** @var Message $message */ | |
$message = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE); | |
$this->logger->err( | |
sprintf('Error handling message \'%s\': %s', $message->messageName(), $exception->getMessage()), | |
[ | |
'message' => $message, | |
'exception' => $exception, | |
'_causation_id' => $message->uuid()->toString(), | |
'causationData' => IdentityCausationData::fromMessage($message), | |
] | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment