Skip to content

Commit 102f108

Browse files
committed
fix interpreting user errors as not found errors
1 parent 318b8db commit 102f108

3 files changed

Lines changed: 57 additions & 10 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Fixed
6+
7+
- User generated errors were interpreted as "not found" errors.
8+
39
## 4.0.1 - 2026-03-19
410

511
### Fixed

‎src/Http/Router.php‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
use Innmind\Http\{
77
ServerRequest,
88
Response,
9-
Response\StatusCode,
109
};
1110
use Innmind\Router\{
1211
Component,
1312
Router as Route,
1413
Any,
1514
Handle,
1615
Respond,
16+
Exception\NotFound,
17+
Exception\NoRouteProvided,
1718
};
1819
use Innmind\Immutable\{
1920
Maybe,
@@ -45,24 +46,28 @@ public function __construct(
4546
public function __invoke(ServerRequest $request): Attempt
4647
{
4748
$recover = $this->recover;
49+
$notFound = $this->notFound;
4850

4951
/**
5052
* @psalm-suppress MixedArgumentTypeCoercion
5153
*/
5254
$route = Route::of(
5355
Any::from($this->routes)
56+
->mapError(static fn($e) => match (true) {
57+
$e instanceof NoRouteProvided => new NotFound,
58+
default => $e,
59+
})
60+
->otherwise(static fn(\Throwable $e) => Component::of(
61+
static fn($request) => $notFound
62+
->filter(static fn() => $e instanceof NotFound)
63+
->match(
64+
static fn($handle) => $handle($request),
65+
static fn() => Attempt::error($e),
66+
),
67+
))
5468
->otherwise(Respond::withHttpErrors())
5569
->otherwise(static fn($e) => Handle::via(
5670
static fn($request) => $recover($request, $e),
57-
))
58-
->or(Handle::via(
59-
fn($request, SideEffect $_) => $this->notFound->match(
60-
static fn($handle) => $handle($request),
61-
static fn() => Attempt::result(Response::of(
62-
StatusCode::notFound,
63-
$request->protocolVersion(),
64-
)),
65-
),
6671
)),
6772
);
6873

‎tests/ApplicationTest.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,4 +1406,40 @@ public function testRecoverRouteError(): BlackBox\Proof
14061406
$this->assertSame($expected, $response);
14071407
});
14081408
}
1409+
1410+
#[\Innmind\BlackBox\PHPUnit\Framework\Attributes\Group('wip')]
1411+
public function testRouteErrorIsNotSwallowed(): BlackBox\Proof
1412+
{
1413+
return $this
1414+
->forAll(
1415+
Set::of(...ProtocolVersion::cases()),
1416+
Set::sequence(
1417+
Set::compose(
1418+
static fn($key, $value) => [$key, $value],
1419+
Set::strings()->randomize(),
1420+
Set::strings(),
1421+
),
1422+
)->between(0, 10),
1423+
)
1424+
->prove(function($protocol, $variables) {
1425+
$expected = new \Exception;
1426+
1427+
$app = Application::http(Factory::build(), Environment::test($variables))
1428+
->service(Services::serviceA, static fn() => static fn() => Attempt::error($expected))
1429+
->routes(Routes::class);
1430+
1431+
$error = $app
1432+
->run(ServerRequest::of(
1433+
Url::of('/foo'),
1434+
Method::get,
1435+
$protocol,
1436+
))
1437+
->match(
1438+
static fn() => null,
1439+
static fn($e) => $e,
1440+
);
1441+
1442+
$this->assertSame($expected, $error);
1443+
});
1444+
}
14091445
}

0 commit comments

Comments
 (0)