Skip to content

Commit dfe388d

Browse files
committed
Improve error reporting for invalid responses by using ResponseException
1 parent 2748115 commit dfe388d

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,17 @@ The `error` event will be emitted when the EventSource connection fails.
193193
The event receives a single `Exception` argument for the error instance.
194194

195195
```php
196-
$redis->on('error', function (Exception $e) {
196+
$es->on('error', function (Exception $e) {
197197
echo 'Error: ' . $e->getMessage() . PHP_EOL;
198198
});
199199
```
200200

201201
The EventSource connection will be retried automatically when it is temporarily
202202
disconnected. If the server sends a non-successful HTTP status code or an
203203
invalid `Content-Type` response header, the connection will fail permanently.
204+
In this case, the `error` event will receive a
205+
[`ResponseException`](https://github.com/reactphp/http#responseexception)
206+
that provides access to the response via the `getResponse()` method.
204207

205208
```php
206209
$es->on('error', function (Exception $e) use ($es) {

src/EventSource.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use React\EventLoop\Loop;
88
use React\EventLoop\LoopInterface;
99
use React\Http\Browser;
10+
use React\Http\Message\ResponseException;
1011
use React\Stream\ReadableStreamInterface;
1112

1213
/**
@@ -193,7 +194,8 @@ private function request()
193194
$this->request->then(function (ResponseInterface $response) {
194195
if ($response->getStatusCode() !== 200) {
195196
$this->readyState = self::CLOSED;
196-
$this->emit('error', [new \UnexpectedValueException(
197+
$this->emit('error', [new ResponseException(
198+
$response,
197199
'Expected "200 OK" response status, ' . $this->quote($response->getStatusCode() . ' ' . $response->getReasonPhrase()) . ' response status returned'
198200
)]);
199201
$this->close();
@@ -204,7 +206,8 @@ private function request()
204206
$contentType = $response->getHeaderLine('Content-Type');
205207
if (!preg_match('/^text\/event-stream(?:$|;)/i', $contentType)) {
206208
$this->readyState = self::CLOSED;
207-
$this->emit('error', [new \UnexpectedValueException(
209+
$this->emit('error', [new ResponseException(
210+
$response,
208211
'Expected "Content-Type: text/event-stream" response header, ' . (!$response->hasHeader('Content-Type') ? 'no "Content-Type"' : $this->quote('Content-Type: ' . $contentType)) . ' response header returned'
209212
)]);
210213
$this->close();

tests/EventSourceTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,10 @@ public function testConstructorWillReportFatalErrorWhenGetResponseResolvesWithIn
268268
$deferred->resolve($response);
269269

270270
$this->assertEquals(EventSource::CLOSED, $readyState);
271-
$this->assertInstanceOf('UnexpectedValueException', $caught);
271+
$this->assertInstanceOf('React\Http\Message\ResponseException', $caught);
272272
$this->assertEquals($expectedMessage, $caught->getMessage());
273+
$this->assertEquals($response->getStatusCode(), $caught->getCode());
274+
$this->assertSame($response, $caught->getResponse());
273275
}
274276

275277
public function provideInvalidContentType()
@@ -320,8 +322,9 @@ public function testConstructorWillReportFatalErrorWhenGetResponseResolvesWithIn
320322
$deferred->resolve($response);
321323

322324
$this->assertEquals(EventSource::CLOSED, $readyState);
323-
$this->assertInstanceOf('UnexpectedValueException', $caught);
325+
$this->assertInstanceOf('React\Http\Message\ResponseException', $caught);
324326
$this->assertEquals($expectedMessage, $caught->getMessage());
327+
$this->assertSame($response, $caught->getResponse());
325328
}
326329

327330
public function testConstructorWillReportOpenWhenGetResponseResolvesWithValidResponse()

0 commit comments

Comments
 (0)