Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request in exception #493

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Io/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function onResponse(ResponseInterface $response, RequestInterface $reques

// only status codes 200-399 are considered to be valid, reject otherwise
if ($this->obeySuccessCode && ($response->getStatusCode() < 200 || $response->getStatusCode() >= 400)) {
throw new ResponseException($response);
throw new ResponseException($response, $request);
}

// resolve our initial promise
Expand Down
21 changes: 19 additions & 2 deletions src/Message/ResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace React\Http\Message;

use Psr\Http\Message\RequestInterface;
use RuntimeException;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -18,8 +19,15 @@ final class ResponseException extends RuntimeException
{
private $response;

public function __construct(ResponseInterface $response, $message = null, $code = null, $previous = null)
{
private $request;

public function __construct(
ResponseInterface $response,
RequestInterface $request,
$message = null,
$code = null,
$previous = null
) {
if ($message === null) {
$message = 'HTTP status code ' . $response->getStatusCode() . ' (' . $response->getReasonPhrase() . ')';
}
Expand All @@ -29,6 +37,7 @@ public function __construct(ResponseInterface $response, $message = null, $code
parent::__construct($message, $code, $previous);

$this->response = $response;
$this->request = $request;
}

/**
Expand All @@ -40,4 +49,12 @@ public function getResponse()
{
return $this->response;
}

/**
* @return RequestInterface
*/
public function getRequest()
{
return $this->request;
}
}
5 changes: 4 additions & 1 deletion tests/Message/ResponseExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

use React\Http\Message\ResponseException;
use PHPUnit\Framework\TestCase;
use RingCentral\Psr7\Request;
use RingCentral\Psr7\Response;

class ResponseExceptionTest extends TestCase
{
public function testCtorDefaults()
{
$response = new Response();
$request = new Request('get', 'https://example.com/');
$response = $response->withStatus(404, 'File not found');

$e = new ResponseException($response);
$e = new ResponseException($response, $request);

$this->assertEquals(404, $e->getCode());
$this->assertEquals('HTTP status code 404 (File not found)', $e->getMessage());

$this->assertSame($response, $e->getResponse());
$this->assertSame($request, $e->getRequest());
}
}