Skip to content

Commit fb82de1

Browse files
committed
Improve error reporting in examples and documentation
1 parent b011736 commit fb82de1

18 files changed

+134
-27
lines changed

README.md

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
410418
foreach ($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) {
429439
foreach ($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

554571
See 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

examples/01-client-get-request.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99

1010
$client->get('http://google.com/')->then(function (ResponseInterface $response) {
1111
var_dump($response->getHeaders(), (string)$response->getBody());
12+
}, function (Exception $e) {
13+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
1214
});

examples/02-client-concurrent-requests.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99

1010
$client->head('http://www.github.com/clue/http-react')->then(function (ResponseInterface $response) {
1111
var_dump($response->getHeaders(), (string)$response->getBody());
12+
}, function (Exception $e) {
13+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
1214
});
1315

1416
$client->get('http://google.com/')->then(function (ResponseInterface $response) {
1517
var_dump($response->getHeaders(), (string)$response->getBody());
18+
}, function (Exception $e) {
19+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
1620
});
1721

1822
$client->get('http://www.lueck.tv/psocksd')->then(function (ResponseInterface $response) {
1923
var_dump($response->getHeaders(), (string)$response->getBody());
24+
}, function (Exception $e) {
25+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
2026
});

examples/03-client-request-any.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@
2626

2727
var_dump($response->getHeaders());
2828
echo PHP_EOL . $response->getBody();
29+
}, function ($e) {
30+
// Promise v1 and v2 reject with an array of Exceptions here, Promise v3 will use an Exception object instead
31+
if (is_array($e)) {
32+
$e = end($e);
33+
}
34+
assert($e instanceof Exception);
35+
36+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
2937
});

examples/04-client-post-json.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@
2323
json_encode($data)
2424
)->then(function (ResponseInterface $response) {
2525
echo (string)$response->getBody();
26-
}, 'printf');
26+
}, function (Exception $e) {
27+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
28+
});

examples/05-client-put-xml.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@
2020
$xml->asXML()
2121
)->then(function (ResponseInterface $response) {
2222
echo (string)$response->getBody();
23-
}, 'printf');
23+
}, function (Exception $e) {
24+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
25+
});

examples/11-client-http-proxy.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@
2626
// demo fetching HTTP headers (or bail out otherwise)
2727
$browser->get('https://www.google.com/')->then(function (ResponseInterface $response) {
2828
echo RingCentral\Psr7\str($response);
29-
}, 'printf');
29+
}, function (Exception $e) {
30+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
31+
});

examples/12-client-socks-proxy.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@
2626
// demo fetching HTTP headers (or bail out otherwise)
2727
$browser->get('https://www.google.com/')->then(function (ResponseInterface $response) {
2828
echo RingCentral\Psr7\str($response);
29-
}, 'printf');
29+
}, function (Exception $e) {
30+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
31+
});

examples/13-client-ssh-proxy.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222
// demo fetching HTTP headers (or bail out otherwise)
2323
$browser->get('https://www.google.com/')->then(function (ResponseInterface $response) {
2424
echo RingCentral\Psr7\str($response);
25-
}, 'printf');
25+
}, function (Exception $e) {
26+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
27+
});

examples/14-client-unix-domain-sockets.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919
// demo fetching HTTP headers (or bail out otherwise)
2020
$browser->get('http://localhost/info')->then(function (ResponseInterface $response) {
2121
echo Psr7\str($response);
22-
}, 'printf');
22+
}, function (Exception $e) {
23+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
24+
});

0 commit comments

Comments
 (0)