/** * Intercept the exception and inject a response * * @param ResponseInterface $response Response to set */ public function intercept(ResponseInterface $response) { $this->stopPropagation(); $this->getTransaction()->setResponse($response); $this->exception->setThrowImmediately(false); RequestEvents::emitComplete($this->getTransaction()); }
/** * Transfers an HTTP request and populates a response * * @param TransactionInterface $transaction Transaction abject to populate * * @return ResponseInterface */ public function send(TransactionInterface $transaction) { // HTTP/1.1 streams using the PHP stream wrapper require a // Connection: close header. Setting here so that it is added before // emitting the request.before_send event. $request = $transaction->getRequest(); if ($request->getProtocolVersion() == '1.1' && !$request->hasHeader('Connection')) { $transaction->getRequest()->setHeader('Connection', 'close'); } try { RequestEvents::emitBefore($transaction); if (!$transaction->getResponse()) { $this->createResponse($transaction); RequestEvents::emitComplete($transaction); } return $transaction->getResponse(); } catch (RequestException $e) { if ($e->hasResponse() && $e->getResponse()->getBody()) { $message = trim($e->getResponse()->getBody()->__toString()); if (empty($message)) { $message = $e->getMessage(); } throw new APIException($message, $e->getRequest(), $e->getResponse(), $e); } throw $e; } }
public function send(TransactionInterface $transaction) { // HTTP/1.1 streams using the PHP stream wrapper require a // Connection: close header. Setting here so that it is added before // emitting the request.before_send event. $request = $transaction->getRequest(); if ($request->getProtocolVersion() == '1.1' && !$request->hasHeader('Connection')) { $transaction->getRequest()->setHeader('Connection', 'close'); } RequestEvents::emitBefore($transaction); if (!$transaction->getResponse()) { $this->createResponse($transaction); RequestEvents::emitComplete($transaction); } return $transaction->getResponse(); }
public function send(TransactionInterface $transaction) { RequestEvents::emitBefore($transaction); if (!$transaction->getResponse()) { $response = is_callable($this->response) ? call_user_func($this->response, $transaction) : $this->response; if (!$response instanceof ResponseInterface) { throw new \RuntimeException('Invalid mocked response'); } // Read the request body if it is present if ($transaction->getRequest()->getBody()) { $transaction->getRequest()->getBody()->__toString(); } $transaction->setResponse($response); RequestEvents::emitHeaders($transaction); RequestEvents::emitComplete($transaction); } return $transaction->getResponse(); }
public function testEmitsAfterSendEventAndEmitsErrorIfNeeded() { $ex2 = $res = null; $request = new Request('GET', '/'); $t = new Transaction(new Client(), $request); $t->setResponse(new Response(200)); $ex = new RequestException('foo', $request); $t->getRequest()->getEmitter()->on('complete', function ($e) use($ex) { $ex->e = $e; throw $ex; }); $t->getRequest()->getEmitter()->on('error', function ($e) use(&$ex2) { $ex2 = $e->getException(); $e->stopPropagation(); }); RequestEvents::emitComplete($t); $this->assertSame($ex, $ex2); }
public function send(TransactionInterface $transaction) { RequestEvents::emitBefore($transaction); if ($response = $transaction->getResponse()) { return $response; } $factory = $this->curlFactory; $handle = $factory($transaction, $this->messageFactory, $this->checkoutEasyHandle()); curl_exec($handle); $info = curl_getinfo($handle); $info['curl_result'] = curl_errno($handle); if ($info['curl_result']) { $this->handleError($transaction, $info, $handle); } else { $this->releaseEasyHandle($handle); RequestEvents::emitComplete($transaction, $info); } return $transaction->getResponse(); }
private function processResponse(TransactionInterface $transaction, array $curl, BatchContext $context) { $info = $context->removeTransaction($transaction); try { if (!$this->isCurlException($transaction, $curl, $context, $info) && $this->validateResponseWasSet($transaction, $context)) { RequestEvents::emitComplete($transaction, $info); } } catch (\Exception $e) { $this->throwException($e, $context); } }