/**
  * {@inheritdoc}
  */
 protected function doSendInternalRequest(InternalRequestInterface $internalRequest)
 {
     try {
         $this->eventDispatcher->dispatch(Events::REQUEST_CREATED, $requestCreatedEvent = new RequestCreatedEvent($this, $internalRequest));
         $response = parent::doSendInternalRequest($requestCreatedEvent->getRequest());
         $this->eventDispatcher->dispatch(Events::REQUEST_SENT, $requestSentEvent = new RequestSentEvent($this, $requestCreatedEvent->getRequest(), $response));
         if ($requestSentEvent->hasException()) {
             throw $requestSentEvent->getException();
         }
         $response = $requestSentEvent->getResponse();
     } catch (HttpAdapterException $e) {
         $e->setRequest($internalRequest);
         $e->setResponse(isset($response) ? $response : null);
         $this->eventDispatcher->dispatch(Events::REQUEST_ERRORED, $exceptionEvent = new RequestErroredEvent($this, $e));
         if ($exceptionEvent->hasResponse()) {
             return $exceptionEvent->getResponse();
         }
         throw $exceptionEvent->getException();
     }
     return $response;
 }
 /**
  * {@inheritdoc}
  */
 public function onRequestErrored(RequestErroredEvent $event)
 {
     if (($request = $this->retry->retry($event->getException()->getRequest())) === false) {
         return;
     }
     $event->getException()->setRequest($request);
     try {
         $event->setResponse($event->getHttpAdapter()->sendRequest($request));
     } catch (HttpAdapterException $e) {
         $event->setException($e);
     }
 }
 /**
  * On request errored event.
  *
  * @param \Ivory\HttpAdapter\Event\RequestErroredEvent $event The request errored event.
  */
 public function onRequestErrored(RequestErroredEvent $event)
 {
     if ($event->getException()->hasResponse()) {
         $this->cookieJar->extract($event->getException()->getRequest(), $event->getException()->getResponse());
     }
 }
 /**
  * On request errored event.
  *
  * @param \Ivory\HttpAdapter\Event\RequestErroredEvent $event The request errored event.
  */
 public function onRequestErrored(RequestErroredEvent $event)
 {
     $event->getException()->setRequest($this->error($event->getHttpAdapter(), $event->getException()));
 }
 /**
  * On request errored event.
  *
  * @param \Ivory\HttpAdapter\Event\RequestErroredEvent $event The event.
  */
 public function onRequestErrored(RequestErroredEvent $event)
 {
     $this->stopwatch->stop($this->getStopwatchName($event->getHttpAdapter(), $event->getException()->getRequest()));
 }
 /**
  * We arrive here when the request has successfully been intercepted.
  *
  * @param RequestErroredEvent $event The exception event.
  */
 public function onException(RequestErroredEvent $event)
 {
     if (!$this->isRecording) {
         return;
     }
     $exception = $event->getException();
     $request = $exception->getRequest();
     if (!$this->currentTape->hasTrackForRequest($request)) {
         return;
     }
     $track = $this->currentTape->getTrackForRequest($request);
     if (!$exception instanceof TapeRecorderException) {
         // Normal exception, let's store it in the track for the next time
         $this->currentTape->finishRecording($track, $event->getResponse(), $event->getException());
         return;
     }
     // We are in replay mode
     if ($track->hasResponse()) {
         $event->setResponse($track->getResponse());
     }
     if ($track->hasException()) {
         $event->setException($track->getException());
     }
 }