fix(ApiExceptionRenderer): update response for ValidationException

Changed the response status and title for ValidationException from 400 to 422 to align with HTTP standards.
This commit is contained in:
Jérémy Munsch 2026-08-13 12:34:32 +02:00
parent b3f02510b5
commit 8e96abc88d
Signed by: forge-joniko
GPG key ID: D7EFE2F5CB6CC0E4
2 changed files with 26 additions and 11 deletions

View file

@ -33,9 +33,9 @@ final readonly class ApiExceptionRenderer
], 404, ['Content-Type' => self::CONTENT_TYPE]),
$e instanceof ValidationException => response()->json([
'type' => (string) $baseUri->withPath('/errors/400'),
'title' => 'Bad Request',
'status' => 400,
'type' => (string) $baseUri->withPath('/errors/422'),
'title' => 'Unprocessable Entity',
'status' => 422,
'detail' => $e->getMessage(),
'invalidParams' => array_values(array_map(
static function (int | string $field, mixed $messages): array {
@ -56,7 +56,7 @@ final readonly class ApiExceptionRenderer
array_keys($e->errors()),
array_values($e->errors())
)),
], 400, ['Content-Type' => self::CONTENT_TYPE]),
], 422, ['Content-Type' => self::CONTENT_TYPE]),
$e instanceof \DomainException => response()->json([
'type' => (string) $baseUri->withPath('/errors/422'),

View file

@ -4,23 +4,38 @@ declare(strict_types=1);
namespace Tests\Feature;
use Symfony\Component\HttpFoundation\Response;
describe('Error Documentation Pages', function (): void {
it('renders the succinct documentation view for valid error codes', function (): void {
it('renders the documentation view for all valid HTTP error codes', function (int $code): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
$code = 404;
$test = $this;
$expectedTitle = Response::$statusTexts[$code] ?? 'Error';
// WHEN
$response = $test->get("/errors/{$code}");
// THEN
$response->assertStatus(200);
$response->assertSee('Not Found');
$response->assertSee('404');
});
$response->assertSee($expectedTitle);
$response->assertSee((string) $code);
$response->assertSee('Conforme à la spécification RFC 7807 Problem Details');
})->with([400, 404, 422, 429, 500, 503]);
it('returns a 404 for an invalid error code slug', function (): void {
it('returns a 404 when requesting documentation for an unsupported error code', function (int $code): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
// WHEN
$response = $test->get("/errors/{$code}");
// THEN
$response->assertStatus(404);
})->with([0, 418, 999]);
it('returns a 404 for a non-numeric error code slug', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;