public function __construct(puzzle_exception_RequestException $delegate) { parent::__construct($delegate->getMessage(), $delegate->getCode()); $delegateRequest = $delegate->getRequest(); $this->_request = $delegateRequest instanceof tubepress_api_http_message_RequestInterface ? $delegateRequest : new tubepress_http_impl_puzzle_PuzzleBasedRequest($delegateRequest); $delegateResponse = $delegate->getResponse(); if ($delegateResponse !== null) { $this->_response = $delegateResponse instanceof tubepress_api_http_message_ResponseInterface ? $delegateResponse : new tubepress_http_impl_puzzle_PuzzleBasedResponse($delegateResponse); } }
public function testHasThrowState() { $e = puzzle_exception_RequestException::create(new puzzle_message_Request('GET', '/'), new puzzle_message_Response(442)); $this->assertFalse($e->getThrowImmediately()); $e->setThrowImmediately(true); $this->assertTrue($e->getThrowImmediately()); }
/** * Throw a puzzle_exception_RequestException on an HTTP protocol error * * @param puzzle_event_CompleteEvent $event Emitted event * @throws puzzle_exception_RequestException */ public function onComplete(puzzle_event_CompleteEvent $event) { $code = (string) $event->getResponse()->getStatusCode(); // Throw an exception for an unsuccessful response if ($code[0] === '4' || $code[0] === '5') { throw puzzle_exception_RequestException::create($event->getRequest(), $event->getResponse()); } }
public function onSetup() { $this->_mockPuzzleRequest = $this->mock('puzzle_message_RequestInterface'); $this->_mockPuzzleResponse = $this->mock('puzzle_message_ResponseInterface'); $this->_mockPuzzleResponse->shouldReceive('getStatusCode')->times(3)->andReturn(404); $this->_mockPuzzleResponse->shouldReceive('getReasonPhrase')->once()->andReturn('some reason!'); $this->_mockPuzzleResponse->shouldReceive('getEffectiveUrl')->once()->andReturn('http://bar.foo/z/y.php?test=true#frag'); $this->_mockPuzzleRequest->shouldReceive('getUrl')->twice()->andReturn('http://foo.bar/z/y.php?test=true#frag'); $this->_mockPuzzleException = puzzle_exception_RequestException::create($this->_mockPuzzleRequest, $this->_mockPuzzleResponse); $this->_sut = new tubepress_http_impl_puzzle_RequestException($this->_mockPuzzleException); }
public function testInterceptsWithEvent() { $client = new puzzle_Client(); $request = new puzzle_message_Request('GET', '/'); $response = new puzzle_message_Response(404); $transaction = new puzzle_adapter_Transaction($client, $request); $except = new puzzle_exception_RequestException('foo', $request, $response); $event = new puzzle_event_ErrorEvent($transaction, $except); $event->throwImmediately(true); $this->assertTrue($except->getThrowImmediately()); $event->throwImmediately(false); $this->assertFalse($except->getThrowImmediately()); $this->assertSame($except, $event->getException()); $this->assertSame($response, $event->getResponse()); $this->assertSame($request, $event->getRequest()); $this->_closure_testInterceptsWithEvent_res = null; $request->getEmitter()->on('complete', array($this, '__callback_testInterceptsWithEvent')); $good = new puzzle_message_Response(200); $event->intercept($good); $this->assertTrue($event->isPropagationStopped()); $this->assertSame($this->_closure_testInterceptsWithEvent_res->getClient(), $event->getClient()); $this->assertSame($good, $this->_closure_testInterceptsWithEvent_res->getResponse()); }
/** * Emits an error event for a request and accounts for the propagation * of an error event being stopped to prevent the exception from being * thrown. * * @param puzzle_adapter_TransactionInterface $transaction * @param Exception $e * @param array $stats * * @throws puzzle_exception_RequestException */ public static function emitError(puzzle_adapter_TransactionInterface $transaction, Exception $e, array $stats = array()) { $request = $transaction->getRequest(); // Convert non-request exception to a wrapped exception if (!$e instanceof puzzle_exception_RequestException) { $e = new puzzle_exception_RequestException($e->getMessage(), $request, null, $e); } // Mark the exception as having been emitted for an error event. This // works in tandem with the emitBefore method to prevent the error // event from being triggered twice for the same exception. $e->emittedError(true); // Dispatch an event and allow interception if (!$request->getEmitter()->emit('error', new puzzle_event_ErrorEvent($transaction, $e, $stats))->isPropagationStopped()) { throw $e; } }