From b65cb967bebabaa688603d38daba61a0ca033e18 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 13 May 2025 20:41:22 +0200 Subject: [PATCH] Added catch for validation exception specifically Signed-off-by: snipe --- app/Exceptions/Handler.php | 8 +++++++- app/Http/Transformers/DatatablesTransformer.php | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index e387ac9e87..1293efca8d 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -121,7 +121,13 @@ class Handler extends ExceptionHandler // This handles API validation exceptions that happen at the Form Request level, so they // never even get to the controller where we normally nicely format JSON responses - return response()->json(Helper::formatStandardApiResponse('error', null, $e->errors()), 200); + if ($e instanceof ValidationException) { + $response = $this->invalidJson($request, $e); + return response()->json(Helper::formatStandardApiResponse('error', null, $e->errors()), 200); + } + + return response()->json(Helper::formatStandardApiResponse('error', null, 'Undefined exception'), 200); + } diff --git a/app/Http/Transformers/DatatablesTransformer.php b/app/Http/Transformers/DatatablesTransformer.php index 0e69109391..8b61c6cc48 100644 --- a/app/Http/Transformers/DatatablesTransformer.php +++ b/app/Http/Transformers/DatatablesTransformer.php @@ -4,6 +4,10 @@ namespace App\Http\Transformers; class DatatablesTransformer { + + /** + * Transform data for bootstrap tables and API responses for lists of things + **/ public function transformDatatables($objects, $total = null) { (isset($total)) ? $objects_array['total'] = $total : $objects_array['total'] = count($objects); @@ -11,4 +15,15 @@ class DatatablesTransformer return $objects_array; } + + /** + * Transform data for returning the status of items within a bulk action + **/ + public function transformBulkResponseWithStatusAndObjects($objects, $total) + { + (isset($total)) ? $objects_array['total'] = $total : $objects_array['total'] = count($objects); + $objects_array['rows'] = $objects; + + return $objects_array; + } }