Skip to content

Commit 2d471da

Browse files
committed
Improve examples to use proper error handlers
1 parent 2104304 commit 2d471da

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ $connector = new React\Socket\Connector();
8282
$connector->connect('127.0.0.1:8080')->then(function (React\Socket\ConnectionInterface $connection) {
8383
$connection->pipe(new React\Stream\WritableResourceStream(STDOUT));
8484
$connection->write("Hello World!\n");
85+
}, function (Exception $e) {
86+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
8587
});
8688
```
8789

@@ -905,6 +907,8 @@ $connector = new React\Socket\Connector();
905907
$connector->connect($uri)->then(function (React\Socket\ConnectionInterface $connection) {
906908
$connection->write('...');
907909
$connection->end();
910+
}, function (Exception $e) {
911+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
908912
});
909913
```
910914

examples/11-http-client.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@
2929
});
3030

3131
$connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
32-
}, 'printf');
32+
}, function (Exception $e) {
33+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
34+
});

examples/12-https-client.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@
2929
});
3030

3131
$connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
32-
}, 'printf');
32+
}, function (Exception $e) {
33+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
34+
});

examples/21-netcat-client.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
$connection->pipe($stdout);
4949

5050
// report errors to STDERR
51-
$connection->on('error', function ($error) use ($stderr) {
52-
$stderr->write('Stream ERROR: ' . $error . PHP_EOL);
51+
$connection->on('error', function (Exception $e) use ($stderr) {
52+
$stderr->write('Stream error: ' . $e->getMessage() . PHP_EOL);
5353
});
5454

5555
// report closing and stop reading from input
@@ -59,6 +59,6 @@
5959
});
6060

6161
$stderr->write('Connected' . PHP_EOL);
62-
}, function ($error) use ($stderr) {
63-
$stderr->write('Connection ERROR: ' . $error . PHP_EOL);
62+
}, function (Exception $e) use ($stderr) {
63+
$stderr->write('Connection error: ' . $e->getMessage() . PHP_EOL);
6464
});

examples/22-http-client.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@
5353
$connection->pipe($stdout);
5454

5555
$connection->write("GET $resource HTTP/1.0\r\nHost: $host\r\n\r\n");
56-
}, 'printf');
56+
}, function (Exception $e) {
57+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
58+
});

0 commit comments

Comments
 (0)