Skip to content

Commit a872331

Browse files
committed
wip
1 parent 07873ea commit a872331

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

packages/router/src/HandleRouteExceptionMiddleware.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Tempest\Http\Response;
99
use Tempest\Http\Responses\Invalid;
1010
use Tempest\Http\Responses\NotFound;
11+
use Tempest\Router\Exceptions\ConvertsToResponse;
1112
use Tempest\Router\Exceptions\RouteBindingFailed;
1213
use Tempest\Validation\Exceptions\ValidationFailed;
1314

@@ -35,6 +36,8 @@ public function __invoke(Request $request, HttpMiddlewareCallable $next): Respon
3536

3637
try {
3738
return $next($request);
39+
} catch (ConvertsToResponse $exception) {
40+
return $exception->toResponse();
3841
} catch (RouteBindingFailed) {
3942
return new NotFound();
4043
} catch (ValidationFailed $validationException) {

tests/Integration/Http/HttpExceptionHandlerTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,9 @@ public function __construct(FrameworkKernel $kernel)
5353
$this->container = $kernel->container;
5454
}
5555

56-
public static function boot(string $root, array $discoveryLocations = [], ?Container $container = null): self
56+
public static function boot(string $root, array $discoveryLocations = [], ?Container $container = null): FrameworkKernel
5757
{
58-
// This is just to make static analysis pass, this is never called.
59-
// @mago-expect analysis/undefined-function-or-method
60-
// @phpstan-ignore-next-line
61-
return Kernel::boot($root, $discoveryLocations, $container);
58+
return FrameworkKernel::boot($root, $discoveryLocations, $container);
6259
}
6360

6461
public function shutdown(int|string $status = ''): never
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Tests\Tempest\Integration\Route\Fixtures;
4+
5+
use Exception;
6+
use Tempest\Http\Response;
7+
use Tempest\Http\Responses\Ok;
8+
use Tempest\Router\Exceptions\ConvertsToResponse;
9+
10+
final class ConvertsToResponseException extends Exception implements ConvertsToResponse
11+
{
12+
public function __construct(
13+
private readonly string $name,
14+
)
15+
{
16+
parent::__construct($name);
17+
}
18+
19+
public function toResponse(): Response
20+
{
21+
return new Ok($this->name);
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Tests\Tempest\Integration\Route\Fixtures;
4+
5+
use Tempest\Router\Get;
6+
7+
final class ConvertsToResponseExceptionController
8+
{
9+
#[Get('/converts-to-response-exception')]
10+
public function __invoke()
11+
{
12+
throw new ConvertsToResponseException('Test');
13+
}
14+
}

tests/Integration/Route/NotFoundTest.php renamed to tests/Integration/Route/HandleRouteExceptionMiddlewareTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use Tempest\Router\RouteConfig;
66
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
7+
use Tests\Tempest\Integration\Route\Fixtures\ConvertsToResponseExceptionController;
78
use Tests\Tempest\Integration\Route\Fixtures\CustomNotFoundMiddleware;
89

910
/**
1011
* @property \Tempest\Framework\Testing\Http\HttpRouterTester $http
1112
*/
12-
final class NotFoundTest extends FrameworkIntegrationTestCase
13+
final class HandleRouteExceptionMiddlewareTest extends FrameworkIntegrationTestCase
1314
{
1415
public function test_unmatched_route_returns_not_found(): void
1516
{
@@ -23,4 +24,14 @@ public function test_custom_not_found_middleware(): void
2324

2425
$this->http->get('unknown-route')->assertHasHeader('x-not-found');
2526
}
27+
28+
public function test_exception_implementing_converts_to_response(): void
29+
{
30+
$this->registerRoute(ConvertsToResponseExceptionController::class);
31+
32+
$this->http
33+
->get('/converts-to-response-exception')
34+
->assertOk()
35+
->assertSee('Test');
36+
}
2637
}

0 commit comments

Comments
 (0)