Skip to content

Commit 80e4593

Browse files
authored
Merge pull request #427 from clue-labs/error-docs
2 parents 10b4594 + fb82de1 commit 80e4593

18 files changed

+134
-27
lines changed

README.md

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ $client = new React\Http\Browser();
9595

9696
$client->get('http://www.google.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
9797
var_dump($response->getHeaders(), (string)$response->getBody());
98+
}, function (Exception $e) {
99+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
98100
});
99101
```
100102

@@ -179,8 +181,8 @@ $browser->get($url)->then(
179181
function (Psr\Http\Message\ResponseInterface $response) {
180182
var_dump('Response received', $response);
181183
},
182-
function (Exception $error) {
183-
var_dump('There was an error', $error->getMessage());
184+
function (Exception $e) {
185+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
184186
}
185187
);
186188
```
@@ -240,6 +242,8 @@ $browser = $browser->withTimeout(10.0);
240242
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
241243
// response received within 10 seconds maximum
242244
var_dump($response->getHeaders());
245+
}, function (Exception $e) {
246+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
243247
});
244248
```
245249

@@ -327,6 +331,8 @@ The promise will be fulfilled with the last response from the chain of redirects
327331
$browser->get($url, $headers)->then(function (Psr\Http\Message\ResponseInterface $response) {
328332
// the final response will end up here
329333
var_dump($response->getHeaders());
334+
}, function (Exception $e) {
335+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
330336
});
331337
```
332338

@@ -355,6 +361,8 @@ $browser = $browser->withFollowRedirects(false);
355361
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
356362
// any redirects will now end up here
357363
var_dump($response->getHeaders());
364+
}, function (Exception $e) {
365+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
358366
});
359367
```
360368

@@ -418,6 +426,8 @@ from your side.
418426
foreach ($urls as $url) {
419427
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
420428
var_dump($response->getHeaders());
429+
}, function (Exception $e) {
430+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
421431
});
422432
}
423433
```
@@ -437,6 +447,8 @@ $q = new Clue\React\Mq\Queue(10, null, function ($url) use ($browser) {
437447
foreach ($urls as $url) {
438448
$q($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
439449
var_dump($response->getHeaders());
450+
}, function (Exception $e) {
451+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
440452
});
441453
}
442454
```
@@ -489,13 +501,15 @@ $browser->requestStreaming('GET', $url)->then(function (Psr\Http\Message\Respons
489501
echo $chunk;
490502
});
491503

492-
$body->on('error', function (Exception $error) {
493-
echo 'Error: ' . $error->getMessage() . PHP_EOL;
504+
$body->on('error', function (Exception $e) {
505+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
494506
});
495507

496508
$body->on('close', function () {
497509
echo '[DONE]' . PHP_EOL;
498510
});
511+
}, function (Exception $e) {
512+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
499513
});
500514
```
501515

@@ -557,6 +571,9 @@ $stream = download($browser, $url);
557571
$stream->on('data', function ($data) {
558572
echo $data;
559573
});
574+
$stream->on('error', function (Exception $e) {
575+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
576+
});
560577
```
561578

562579
See also the [`requestStreaming()`](#requeststreaming) method for more details.
@@ -573,6 +590,8 @@ to the [request methods](#request-methods) like this:
573590
```php
574591
$browser->post($url, array(), $stream)->then(function (Psr\Http\Message\ResponseInterface $response) {
575592
echo 'Successfully sent.';
593+
}, function (Exception $e) {
594+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
576595
});
577596
```
578597

@@ -691,6 +710,8 @@ $browser = new React\Http\Browser($connector);
691710

692711
$client->get('http://localhost/info')->then(function (Psr\Http\Message\ResponseInterface $response) {
693712
var_dump($response->getHeaders(), (string)$response->getBody());
713+
}, function (Exception $e) {
714+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
694715
});
695716
```
696717

@@ -1193,13 +1214,13 @@ $http = new React\Http\HttpServer(
11931214
});
11941215

11951216
// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
1196-
$body->on('error', function (\Exception $exception) use ($resolve, &$bytes) {
1217+
$body->on('error', function (Exception $e) use ($resolve, &$bytes) {
11971218
$resolve(new React\Http\Message\Response(
11981219
400,
11991220
array(
12001221
'Content-Type' => 'text/plain'
12011222
),
1202-
"Encountered error after $bytes bytes: {$exception->getMessage()}\n"
1223+
"Encountered error after $bytes bytes: {$e->getMessage()}\n"
12031224
));
12041225
});
12051226
});
@@ -1922,6 +1943,8 @@ send an HTTP GET request.
19221943
```php
19231944
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
19241945
var_dump((string)$response->getBody());
1946+
}, function (Exception $e) {
1947+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
19251948
});
19261949
```
19271950

@@ -1941,6 +1964,8 @@ $browser->post(
19411964
json_encode($data)
19421965
)->then(function (Psr\Http\Message\ResponseInterface $response) {
19431966
var_dump(json_decode((string)$response->getBody()));
1967+
}, function (Exception $e) {
1968+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
19441969
});
19451970
```
19461971

@@ -1986,6 +2011,8 @@ send an HTTP HEAD request.
19862011
```php
19872012
$browser->head($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
19882013
var_dump($response->getHeaders());
2014+
}, function (Exception $e) {
2015+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
19892016
});
19902017
```
19912018

@@ -2003,6 +2030,8 @@ $browser->patch(
20032030
json_encode($data)
20042031
)->then(function (Psr\Http\Message\ResponseInterface $response) {
20052032
var_dump(json_decode((string)$response->getBody()));
2033+
}, function (Exception $e) {
2034+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
20062035
});
20072036
```
20082037

@@ -2035,6 +2064,8 @@ $browser->put(
20352064
$xml->asXML()
20362065
)->then(function (Psr\Http\Message\ResponseInterface $response) {
20372066
var_dump((string)$response->getBody());
2067+
}, function (Exception $e) {
2068+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
20382069
});
20392070
```
20402071

@@ -2063,6 +2094,8 @@ send an HTTP DELETE request.
20632094
```php
20642095
$browser->delete($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
20652096
var_dump((string)$response->getBody());
2097+
}, function (Exception $e) {
2098+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
20662099
});
20672100
```
20682101

@@ -2081,6 +2114,8 @@ can use this method:
20812114
```php
20822115
$browser->request('OPTIONS', $url)->then(function (Psr\Http\Message\ResponseInterface $response) {
20832116
var_dump((string)$response->getBody());
2117+
}, function (Exception $e) {
2118+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
20842119
});
20852120
```
20862121

@@ -2132,13 +2167,15 @@ $browser->requestStreaming('GET', $url)->then(function (Psr\Http\Message\Respons
21322167
echo $chunk;
21332168
});
21342169

2135-
$body->on('error', function (Exception $error) {
2136-
echo 'Error: ' . $error->getMessage() . PHP_EOL;
2170+
$body->on('error', function (Exception $e) {
2171+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
21372172
});
21382173

21392174
$body->on('close', function () {
21402175
echo '[DONE]' . PHP_EOL;
21412176
});
2177+
}, function (Exception $e) {
2178+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
21422179
});
21432180
```
21442181

@@ -2217,6 +2254,8 @@ $browser = $browser->withFollowRedirects(0);
22172254
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
22182255
// only non-redirected responses will now end up here
22192256
var_dump($response->getHeaders());
2257+
}, function (Exception $e) {
2258+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
22202259
});
22212260
```
22222261

@@ -2230,6 +2269,8 @@ $browser = $browser->withFollowRedirects(false);
22302269
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
22312270
// any redirects will now end up here
22322271
var_dump($response->getHeaderLine('Location'));
2272+
}, function (Exception $e) {
2273+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
22332274
});
22342275
```
22352276

@@ -2261,6 +2302,8 @@ $browser = $browser->withRejectErrorResponse(false);
22612302
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
22622303
// any HTTP response will now end up here
22632304
var_dump($response->getStatusCode(), $response->getReasonPhrase());
2305+
}, function (Exception $e) {
2306+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
22642307
});
22652308
```
22662309

@@ -2280,7 +2323,7 @@ $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response
22802323
$response = $e->getResponse();
22812324
var_dump($response->getStatusCode(), $response->getReasonPhrase());
22822325
} else {
2283-
var_dump($e->getMessage());
2326+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
22842327
}
22852328
});
22862329
```
@@ -2370,6 +2413,8 @@ $browser = $browser->withResponseBuffer(1024 * 1024);
23702413
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
23712414
// response body will not exceed 1 MiB
23722415
var_dump($response->getHeaders(), (string) $response->getBody());
2416+
}, function (Exception $e) {
2417+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
23732418
});
23742419
```
23752420

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)