@@ -91,6 +91,8 @@ $client = new React\Http\Browser();
9191
9292$client->get('http://www.google.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
9393 var_dump($response->getHeaders(), (string)$response->getBody());
94+ }, function (Exception $e) {
95+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
9496});
9597```
9698
@@ -171,8 +173,8 @@ $browser->get($url)->then(
171173 function (Psr\Http\Message\ResponseInterface $response) {
172174 var_dump('Response received', $response);
173175 },
174- function (Exception $error ) {
175- var_dump('There was an error', $error ->getMessage()) ;
176+ function (Exception $e ) {
177+ echo 'Error: ' . $e ->getMessage() . PHP_EOL ;
176178 }
177179);
178180```
@@ -232,6 +234,8 @@ $browser = $browser->withTimeout(10.0);
232234$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
233235 // response received within 10 seconds maximum
234236 var_dump($response->getHeaders());
237+ }, function (Exception $e) {
238+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
235239});
236240```
237241
@@ -319,6 +323,8 @@ The promise will be fulfilled with the last response from the chain of redirects
319323$browser->get($url, $headers)->then(function (Psr\Http\Message\ResponseInterface $response) {
320324 // the final response will end up here
321325 var_dump($response->getHeaders());
326+ }, function (Exception $e) {
327+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
322328});
323329```
324330
@@ -347,6 +353,8 @@ $browser = $browser->withFollowRedirects(false);
347353$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
348354 // any redirects will now end up here
349355 var_dump($response->getHeaders());
356+ }, function (Exception $e) {
357+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
350358});
351359```
352360
@@ -410,6 +418,8 @@ from your side.
410418foreach ($urls as $url) {
411419 $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
412420 var_dump($response->getHeaders());
421+ }, function (Exception $e) {
422+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
413423 });
414424}
415425```
@@ -429,6 +439,8 @@ $q = new Clue\React\Mq\Queue(10, null, function ($url) use ($browser) {
429439foreach ($urls as $url) {
430440 $q($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
431441 var_dump($response->getHeaders());
442+ }, function (Exception $e) {
443+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
432444 });
433445}
434446```
@@ -481,13 +493,15 @@ $browser->requestStreaming('GET', $url)->then(function (Psr\Http\Message\Respons
481493 echo $chunk;
482494 });
483495
484- $body->on('error', function (Exception $error ) {
485- echo 'Error: ' . $error ->getMessage() . PHP_EOL;
496+ $body->on('error', function (Exception $e ) {
497+ echo 'Error: ' . $e ->getMessage() . PHP_EOL;
486498 });
487499
488500 $body->on('close', function () {
489501 echo '[DONE]' . PHP_EOL;
490502 });
503+ }, function (Exception $e) {
504+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
491505});
492506```
493507
@@ -549,6 +563,9 @@ $stream = download($browser, $url);
549563$stream->on('data', function ($data) {
550564 echo $data;
551565});
566+ $stream->on('error', function (Exception $e) {
567+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
568+ });
552569```
553570
554571See also the [ ` requestStreaming() ` ] ( #requeststreaming ) method for more details.
@@ -565,6 +582,8 @@ to the [request methods](#request-methods) like this:
565582``` php
566583$browser->post($url, array(), $stream)->then(function (Psr\Http\Message\ResponseInterface $response) {
567584 echo 'Successfully sent.';
585+ }, function (Exception $e) {
586+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
568587});
569588```
570589
@@ -683,6 +702,8 @@ $browser = new React\Http\Browser($connector);
683702
684703$client->get('http://localhost/info')->then(function (Psr\Http\Message\ResponseInterface $response) {
685704 var_dump($response->getHeaders(), (string)$response->getBody());
705+ }, function (Exception $e) {
706+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
686707});
687708```
688709
@@ -1185,13 +1206,13 @@ $http = new React\Http\HttpServer(
11851206 });
11861207
11871208 // an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
1188- $body->on('error', function (\ Exception $exception ) use ($resolve, & $bytes) {
1209+ $body->on('error', function (Exception $e ) use ($resolve, & $bytes) {
11891210 $resolve(new React\Http\Message\Response(
11901211 400,
11911212 array(
11921213 'Content-Type' => 'text/plain'
11931214 ),
1194- "Encountered error after $bytes bytes: {$exception ->getMessage()}\n"
1215+ "Encountered error after $bytes bytes: {$e ->getMessage()}\n"
11951216 ));
11961217 });
11971218 });
@@ -1914,6 +1935,8 @@ send an HTTP GET request.
19141935``` php
19151936$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
19161937 var_dump((string)$response->getBody());
1938+ }, function (Exception $e) {
1939+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
19171940});
19181941```
19191942
@@ -1933,6 +1956,8 @@ $browser->post(
19331956 json_encode($data)
19341957)->then(function (Psr\Http\Message\ResponseInterface $response) {
19351958 var_dump(json_decode((string)$response->getBody()));
1959+ }, function (Exception $e) {
1960+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
19361961});
19371962```
19381963
@@ -1978,6 +2003,8 @@ send an HTTP HEAD request.
19782003``` php
19792004$browser->head($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
19802005 var_dump($response->getHeaders());
2006+ }, function (Exception $e) {
2007+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
19812008});
19822009```
19832010
@@ -1995,6 +2022,8 @@ $browser->patch(
19952022 json_encode($data)
19962023)->then(function (Psr\Http\Message\ResponseInterface $response) {
19972024 var_dump(json_decode((string)$response->getBody()));
2025+ }, function (Exception $e) {
2026+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
19982027});
19992028```
20002029
@@ -2027,6 +2056,8 @@ $browser->put(
20272056 $xml->asXML()
20282057)->then(function (Psr\Http\Message\ResponseInterface $response) {
20292058 var_dump((string)$response->getBody());
2059+ }, function (Exception $e) {
2060+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
20302061});
20312062```
20322063
@@ -2055,6 +2086,8 @@ send an HTTP DELETE request.
20552086``` php
20562087$browser->delete($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
20572088 var_dump((string)$response->getBody());
2089+ }, function (Exception $e) {
2090+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
20582091});
20592092```
20602093
@@ -2073,6 +2106,8 @@ can use this method:
20732106``` php
20742107$browser->request('OPTIONS', $url)->then(function (Psr\Http\Message\ResponseInterface $response) {
20752108 var_dump((string)$response->getBody());
2109+ }, function (Exception $e) {
2110+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
20762111});
20772112```
20782113
@@ -2124,13 +2159,15 @@ $browser->requestStreaming('GET', $url)->then(function (Psr\Http\Message\Respons
21242159 echo $chunk;
21252160 });
21262161
2127- $body->on('error', function (Exception $error ) {
2128- echo 'Error: ' . $error ->getMessage() . PHP_EOL;
2162+ $body->on('error', function (Exception $e ) {
2163+ echo 'Error: ' . $e ->getMessage() . PHP_EOL;
21292164 });
21302165
21312166 $body->on('close', function () {
21322167 echo '[DONE]' . PHP_EOL;
21332168 });
2169+ }, function (Exception $e) {
2170+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
21342171});
21352172```
21362173
@@ -2209,6 +2246,8 @@ $browser = $browser->withFollowRedirects(0);
22092246$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
22102247 // only non-redirected responses will now end up here
22112248 var_dump($response->getHeaders());
2249+ }, function (Exception $e) {
2250+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
22122251});
22132252```
22142253
@@ -2222,6 +2261,8 @@ $browser = $browser->withFollowRedirects(false);
22222261$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
22232262 // any redirects will now end up here
22242263 var_dump($response->getHeaderLine('Location'));
2264+ }, function (Exception $e) {
2265+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
22252266});
22262267```
22272268
@@ -2253,6 +2294,8 @@ $browser = $browser->withRejectErrorResponse(false);
22532294$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
22542295 // any HTTP response will now end up here
22552296 var_dump($response->getStatusCode(), $response->getReasonPhrase());
2297+ }, function (Exception $e) {
2298+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
22562299});
22572300```
22582301
@@ -2272,7 +2315,7 @@ $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response
22722315 $response = $e->getResponse();
22732316 var_dump($response->getStatusCode(), $response->getReasonPhrase());
22742317 } else {
2275- var_dump( $e->getMessage()) ;
2318+ echo 'Error: ' . $e->getMessage() . PHP_EOL ;
22762319 }
22772320});
22782321```
@@ -2362,6 +2405,8 @@ $browser = $browser->withResponseBuffer(1024 * 1024);
23622405$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
23632406 // response body will not exceed 1 MiB
23642407 var_dump($response->getHeaders(), (string) $response->getBody());
2408+ }, function (Exception $e) {
2409+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
23652410});
23662411```
23672412
0 commit comments