Skip to content

Commit e863683

Browse files
committed
Treat validation errors the same as a normal error; match the output exactly
1 parent 8c98264 commit e863683

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

app/Exceptions/Handler.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,14 @@ public function render($request, Throwable $exception)
169169
*/
170170
public function invalidJson($request, ValidationException $exception)
171171
{
172-
$codes = collect($exception->validator->failed())->mapWithKeys(function ($reasons, $field) {
173-
$cleaned = [];
174-
foreach ($reasons as $reason => $attrs) {
175-
$cleaned[] = snake_case($reason);
176-
}
177-
178-
return [str_replace('.', '_', $field) => $cleaned];
179-
})->toArray();
180-
181-
$errors = collect($exception->errors())->map(function ($errors, $field) use ($codes) {
172+
$errors = collect($exception->errors())->map(function ($errors, $field) use ($exception) {
182173
$response = [];
183174
foreach ($errors as $key => $error) {
184-
$response[] = [
185-
'code' => str_replace(self::PTERODACTYL_RULE_STRING, 'p_', array_get(
186-
$codes, str_replace('.', '_', $field) . '.' . $key
187-
)),
175+
$converted = self::convertToArray($exception)['errors'][0];
176+
$response[] = array_merge($converted, [
188177
'detail' => $error,
189-
'source' => ['field' => $field],
190-
];
178+
'source_field' => $field,
179+
]);
191180
}
192181

193182
return $response;
@@ -209,7 +198,9 @@ public static function convertToArray(Throwable $exception, array $override = []
209198
{
210199
$error = [
211200
'code' => class_basename($exception),
212-
'status' => method_exists($exception, 'getStatusCode') ? strval($exception->getStatusCode()) : '500',
201+
'status' => method_exists($exception, 'getStatusCode')
202+
? strval($exception->getStatusCode())
203+
: ($exception instanceof ValidationException ? '422' : '500'),
213204
'detail' => 'An error was encountered while processing this request.',
214205
];
215206

0 commit comments

Comments
 (0)