/** * @test */ public function it_wraps_a_message_dispatch_exception_and_tracks_pending_commands() { $pendingCommands = ['dispatchMe', 'tellMe']; $actionEvent = new DefaultActionEvent(MessageBus::EVENT_INVOKE_HANDLER); $prevException = new \Exception('previous'); $messageDispatchException = MessageDispatchException::failed($actionEvent, $prevException); $commandDispatchException = CommandDispatchException::wrap($messageDispatchException, $pendingCommands); $this->assertSame($actionEvent, $commandDispatchException->getFailedDispatchEvent()); $this->assertSame($prevException, $commandDispatchException->getPrevious()); $this->assertSame($pendingCommands, $commandDispatchException->getPendingCommands()); }
/** * @param mixed $query * @return Promise */ public function dispatch($query) { $deferred = new Deferred(); $promise = $deferred->promise(); $actionEvent = $this->getActionEventEmitter()->getNewActionEvent(); $actionEvent->setTarget($this); $actionEvent->setParam(self::EVENT_PARAM_DEFERRED, $deferred); try { $this->initialize($query, $actionEvent); if ($actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER) === null) { $actionEvent->setName(self::EVENT_ROUTE); $this->trigger($actionEvent); } if ($actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER) === null) { throw new RuntimeException(sprintf("QueryBus was not able to identify a Finder for query %s", $this->getMessageType($query))); } if (is_string($actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER))) { $actionEvent->setName(self::EVENT_LOCATE_HANDLER); $this->trigger($actionEvent); } $finder = $actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER); if (is_callable($finder)) { $finder($query, $deferred); } else { $actionEvent->setName(self::EVENT_INVOKE_FINDER); $this->trigger($actionEvent); } $this->triggerFinalize($actionEvent); } catch (\Exception $ex) { $failedPhase = $actionEvent->getName(); $actionEvent->setParam(self::EVENT_PARAM_EXCEPTION, $ex); $this->triggerError($actionEvent); $this->triggerFinalize($actionEvent); //Check if a listener has removed the exception to indicate that it was able to handle it if ($ex = $actionEvent->getParam(self::EVENT_PARAM_EXCEPTION)) { $actionEvent->setName($failedPhase); $deferred->reject(MessageDispatchException::failed($actionEvent, $ex)); } } return $promise; }
/** * @param ActionEvent $actionEvent * @param \Exception $ex * @throws Exception\MessageDispatchException */ protected function handleException(ActionEvent $actionEvent, \Exception $ex) { $failedPhase = $actionEvent->getName(); $actionEvent->setParam(self::EVENT_PARAM_EXCEPTION, $ex); $this->triggerError($actionEvent); $this->triggerFinalize($actionEvent); //Check if a listener has removed the exception to indicate that it was able to handle it if ($ex = $actionEvent->getParam(self::EVENT_PARAM_EXCEPTION)) { $actionEvent->setName($failedPhase); throw MessageDispatchException::failed($actionEvent, $ex); } }