Skip to content

Commit 297bbd2

Browse files
wip prettify validation error responses
1 parent 29d4e50 commit 297bbd2

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

packages/router/src/HandleRouteExceptionMiddleware.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
use Tempest\Router\Exceptions\ConvertsToResponse;
1414
use Tempest\Router\Exceptions\RouteBindingFailed;
1515
use Tempest\Validation\Exceptions\ValidationFailed;
16+
use Tempest\Validation\Rule;
17+
use Tempest\Validation\Validator;
18+
19+
use function Tempest\get;
20+
use function Tempest\Support\arr;
1621

1722
#[Priority(Priority::FRAMEWORK - 10)]
1823
final readonly class HandleRouteExceptionMiddleware implements HttpMiddleware
@@ -26,7 +31,9 @@ public function __invoke(Request $request, HttpMiddlewareCallable $next): Respon
2631
if ($this->routeConfig->throwHttpExceptions === true) {
2732
$response = $this->forward($request, $next);
2833

29-
if ($response->status->isServerError() || $response->status->isClientError()) {
34+
$isValidationError = $response->status === Status::UNPROCESSABLE_CONTENT;
35+
36+
if (! $isValidationError && ($response->status->isServerError() || $response->status->isClientError())) {
3037
throw new HttpRequestFailed(
3138
request: $request,
3239
status: $response->status,
@@ -61,9 +68,15 @@ private function forward(Request $request, HttpMiddlewareCallable $next): Respon
6168
return new NotFound();
6269
} catch (ValidationFailed $validationException) {
6370
if ($this->wantsJson($request)) {
71+
$errors = arr($validationException->failingRules)->map(
72+
fn (array $failingRulesForField, string $field) => arr($failingRulesForField)->map(
73+
fn (Rule $rule) => get(Validator::class)->getErrorMessage($rule, $field),
74+
)->toArray(),
75+
);
76+
6477
return new Json([
65-
'message' => $validationException->getMessage(),
66-
'errors' => $validationException->errorMessages,
78+
'message' => $errors->first()[0],
79+
'errors' => $errors->toArray(),
6780
])->setStatus(Status::UNPROCESSABLE_CONTENT);
6881
}
6982

0 commit comments

Comments
 (0)