Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Fixed

- User generated errors were interpreted as "not found" errors.

## 4.0.1 - 2026-03-19

### Fixed
Expand Down
25 changes: 15 additions & 10 deletions src/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use Innmind\Http\{
ServerRequest,
Response,
Response\StatusCode,
};
use Innmind\Router\{
Component,
Router as Route,
Any,
Handle,
Respond,
Exception\NotFound,
Exception\NoRouteProvided,
};
use Innmind\Immutable\{
Maybe,
Expand Down Expand Up @@ -45,24 +46,28 @@ public function __construct(
public function __invoke(ServerRequest $request): Attempt
{
$recover = $this->recover;
$notFound = $this->notFound;

/**
* @psalm-suppress MixedArgumentTypeCoercion
*/
$route = Route::of(
Any::from($this->routes)
->mapError(static fn($e) => match (true) {
$e instanceof NoRouteProvided => new NotFound,
default => $e,
})
->otherwise(static fn(\Throwable $e) => Component::of(
static fn($request) => $notFound
->filter(static fn() => $e instanceof NotFound)
->match(
static fn($handle) => $handle($request),
static fn() => Attempt::error($e),
),
))
->otherwise(Respond::withHttpErrors())
->otherwise(static fn($e) => Handle::via(
static fn($request) => $recover($request, $e),
))
->or(Handle::via(
fn($request, SideEffect $_) => $this->notFound->match(
static fn($handle) => $handle($request),
static fn() => Attempt::result(Response::of(
StatusCode::notFound,
$request->protocolVersion(),
)),
),
)),
);

Expand Down
36 changes: 36 additions & 0 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1406,4 +1406,40 @@ public function testRecoverRouteError(): BlackBox\Proof
$this->assertSame($expected, $response);
});
}

#[\Innmind\BlackBox\PHPUnit\Framework\Attributes\Group('wip')]
public function testRouteErrorIsNotSwallowed(): BlackBox\Proof
{
return $this
->forAll(
Set::of(...ProtocolVersion::cases()),
Set::sequence(
Set::compose(
static fn($key, $value) => [$key, $value],
Set::strings()->randomize(),
Set::strings(),
),
)->between(0, 10),
)
->prove(function($protocol, $variables) {
$expected = new \Exception;

$app = Application::http(Factory::build(), Environment::test($variables))
->service(Services::serviceA, static fn() => static fn() => Attempt::error($expected))
->routes(Routes::class);

$error = $app
->run(ServerRequest::of(
Url::of('/foo'),
Method::get,
$protocol,
))
->match(
static fn() => null,
static fn($e) => $e,
);

$this->assertSame($expected, $error);
});
}
}
Loading