Skip to content

Commit 2d76b77

Browse files
authored
Add request exceptions handling
1 parent 2d91c88 commit 2d76b77

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,37 @@ $factory = new Psr17Factory();
8585
//
8686
$psr7 = new PSR7Worker($worker, $factory, $factory, $factory);
8787

88-
while ($request = $psr7->waitRequest()) {
88+
while (true) {
8989
try {
90+
$request = $psr7->waitRequest();
91+
} catch (\Throwable $e) {
92+
// Although the PSR-17 specification clearly states that there can be
93+
// no exceptions when creating a request, however, some implementations
94+
// may violate this rule. Therefore, it is recommended to process the
95+
// incoming request for errors.
96+
//
97+
// Send "Bad Request" response.
98+
$psr7->respond(new Response(400));
99+
continue;
100+
}
101+
102+
try {
103+
// Here is where the call to your application code will be located.
104+
// For example:
105+
//
106+
// $response = $app->send($request);
107+
//
90108
// Reply by the 200 OK response
91109
$psr7->respond(new Response(200, [], 'Hello RoadRunner!'));
92110
} catch (\Throwable $e) {
93-
111+
// In case of any exceptions in the application code, you should handle
112+
// them and inform the client about the presence of a server error.
113+
//
94114
// Reply by the 500 Internal Server Error response
95115
$psr7->respond(new Response(500, [], 'Something Went Wrong!'));
96116

97-
// Report error to RoadRunner (optional)
117+
// Additionally, we can inform the RoadRunner that the processing
118+
// of the request failed.
98119
$worker->error((string)$e);
99120
}
100121
}

0 commit comments

Comments
 (0)