Skip to content

Commit 1df145e

Browse files
committed
add 404 and 403 routes
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent 7243f70 commit 1df145e

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

examples/simple.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,20 @@
2121
*/
2222
function get_example_inputs(): iterable
2323
{
24-
yield array(HttpMethod::GET, '/');
25-
yield array(HttpMethod::GET, '/user/foo');
26-
yield array(HttpMethod::GET, '/user/bar');
27-
yield array(HttpMethod::GET, '/contact-us');
28-
yield array(HttpMethod::GET, '/about-us');
29-
yield array(HttpMethod::POST, '/');
24+
yield [HttpMethod::GET, '/'];
25+
yield [HttpMethod::GET, '/user/foo'];
26+
yield [HttpMethod::GET, '/user/bar'];
27+
yield [HttpMethod::GET, '/contact-us'];
28+
yield [HttpMethod::GET, '/about-us'];
29+
yield [HttpMethod::POST, '/'];
30+
31+
// known 404
32+
yield [HttpMethod::GET, '/user/31'];
33+
yield [HttpMethod::GET, '/user/HELLO'];
34+
yield [HttpMethod::GET, '/user/Hans8'];
35+
36+
// known 403
37+
yield [HttpMethod::PUT, '/user/azjezz'];
3038
}
3139

3240
(static function (): void {
@@ -60,20 +68,18 @@ function get_example_inputs(): iterable
6068
[$responder, $parameters] = $router->match($method, $path);
6169

6270
$response = $responder($parameters);
63-
64-
$method = Str\pad_right(Str\format('[%s]', $method), 8);
65-
$request = Str\pad_right(Str\format('%s %s', $method, $path), 25);
66-
67-
$output->write(Str\format("%s -> %s\n", $request, $response));
6871
} catch (HttpException\MethodNotAllowedException $e) {
69-
$allowed_methods = $e->getAllowedMethods();
70-
71-
// Handle 403.
72+
$response = 'Error[403]: allowed methods "' . Str\join($e->getAllowedMethods(), '", "') . '"';
7273
} catch (HttpException\NotFoundException) {
73-
// Handle 404.
74+
$response = 'Error[404]';
7475
} catch (HttpException\InternalServerErrorException) {
75-
// Handle 500.
76+
$response = 'Error[500]';
7677
}
78+
79+
$method = Str\pad_right(Str\format('[%s]', $method), 8);
80+
$request = Str\pad_right(Str\format('%s %s', $method, $path), 25);
81+
82+
$output->write(Str\format("%s -> %s\n", $request, $response));
7783
}
7884

7985
exit(0);

0 commit comments

Comments
 (0)