From 31ae87f358c85a78c8605c73135275274f9cf65c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Wed, 12 Aug 2026 17:00:15 +0200 Subject: [PATCH] fix(exceptions): correct status code for validation errors Updated the status code for ValidationException responses from 422 to 400 to align with HTTP standards. --- .../V1/Exceptions/ApiExceptionRenderer.php | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Presentation/Api/V1/Exceptions/ApiExceptionRenderer.php b/src/Presentation/Api/V1/Exceptions/ApiExceptionRenderer.php index f3e9ac6..06416de 100644 --- a/src/Presentation/Api/V1/Exceptions/ApiExceptionRenderer.php +++ b/src/Presentation/Api/V1/Exceptions/ApiExceptionRenderer.php @@ -31,7 +31,7 @@ final readonly class ApiExceptionRenderer $e instanceof ValidationException => response()->json([ 'type' => (string) $baseUri->withPath('/errors/' . ApiErrorCode::ValidationError->value), 'title' => ApiErrorCode::ValidationError->title(), - 'status' => 422, + 'status' => 400, 'detail' => $e->getMessage(), 'invalidParams' => array_values(array_map( static function (int | string $field, mixed $messages): array { @@ -52,15 +52,22 @@ final readonly class ApiExceptionRenderer array_keys($e->errors()), array_values($e->errors()) )), - ], 422, ['Content-Type' => self::CONTENT_TYPE]), + ], 400, ['Content-Type' => self::CONTENT_TYPE]), + $e instanceof \DomainException => response()->json([ - 'type' => (string) $baseUri->withPath('/errors/' . ApiErrorCode::BadRequest->value), - 'title' => ApiErrorCode::BadRequest->title(), - 'status' => 400, - 'detail' => $e->getMessage(), - 'instance' => $request->getPathInfo(), - ], 400, ['Content-Type' => self::CONTENT_TYPE]), + 'type' => (string) $baseUri->withPath('/errors/' . ApiErrorCode::ValidationError->value), + 'title' => ApiErrorCode::ValidationError->title(), + 'status' => 422, + 'detail' => $e->getMessage(), + 'instance' => $request->getPathInfo(), + 'invalidParams' => [ + [ + 'name' => 'domain_invariant', + 'reason' => $e->getMessage(), + ], + ], + ], 422, ['Content-Type' => self::CONTENT_TYPE]), default => response()->json([ 'type' => (string) $baseUri->withPath('/errors/' . ApiErrorCode::InternalError->value),