From 43156e8d5394910981d5f26e28acb1771b16e63b Mon Sep 17 00:00:00 2001 From: DaneEveritt Date: Sun, 26 Jun 2022 16:31:48 -0400 Subject: [PATCH] Improve error messaging for validation exceptions --- .../Model/DataValidationException.php | 32 +++++++++++++++---- .../Controllers/Admin/ServersController.php | 4 +-- app/Models/Model.php | 2 +- .../Eloquent/EloquentRepository.php | 4 +-- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/app/Exceptions/Model/DataValidationException.php b/app/Exceptions/Model/DataValidationException.php index 1118081e5..81582864c 100644 --- a/app/Exceptions/Model/DataValidationException.php +++ b/app/Exceptions/Model/DataValidationException.php @@ -2,6 +2,7 @@ namespace Pterodactyl\Exceptions\Model; +use Illuminate\Database\Eloquent\Model; use Illuminate\Contracts\Validation\Validator; use Pterodactyl\Exceptions\PterodactylException; use Illuminate\Contracts\Support\MessageProvider; @@ -11,21 +12,30 @@ class DataValidationException extends PterodactylException implements HttpExcept { /** * The validator instance. - * - * @var \Illuminate\Contracts\Validation\Validator */ - public $validator; + protected Validator $validator; + + /** + * The underlying model instance that triggered this exception. + */ + protected Model $model; /** * DataValidationException constructor. */ - public function __construct(Validator $validator) + public function __construct(Validator $validator, Model $model) { - parent::__construct( - 'Data integrity exception encountered while performing database write operation. ' . $validator->errors()->toJson() + $message = sprintf( + 'Could not save %s[%s]: failed to validate data: %s', + get_class($model), + $model->getKey(), + $validator->errors()->toJson() ); + parent::__construct($message); + $this->validator = $validator; + $this->model = $model; } /** @@ -55,4 +65,14 @@ class DataValidationException extends PterodactylException implements HttpExcept { return []; } + + public function getValidator(): Validator + { + return $this->validator; + } + + public function getModel(): Model + { + return $this->model; + } } diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index bb36afa18..c226638b9 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -271,7 +271,7 @@ class ServersController extends Controller 'database_limit', 'allocation_limit', 'backup_limit', 'oom_disabled', ])); } catch (DataValidationException $exception) { - throw new ValidationException($exception->validator); + throw new ValidationException($exception->getValidator()); } $this->alert->success(trans('admin/server.alerts.build_updated'))->flash(); @@ -315,7 +315,7 @@ class ServersController extends Controller ->setUserLevel(User::USER_LEVEL_ADMIN) ->handle($server, $data); } catch (DataValidationException $exception) { - throw new ValidationException($exception->validator); + throw new ValidationException($exception->getValidator()); } $this->alert->success(trans('admin/server.alerts.startup_changed'))->flash(); diff --git a/app/Models/Model.php b/app/Models/Model.php index cc8ace6c3..5c3488728 100644 --- a/app/Models/Model.php +++ b/app/Models/Model.php @@ -57,7 +57,7 @@ abstract class Model extends IlluminateModel try { $model->validate(); } catch (ValidationException $exception) { - throw new DataValidationException($exception->validator); + throw new DataValidationException($exception->validator, $model); } return true; diff --git a/app/Repositories/Eloquent/EloquentRepository.php b/app/Repositories/Eloquent/EloquentRepository.php index 3d3e09cdc..6403d2764 100644 --- a/app/Repositories/Eloquent/EloquentRepository.php +++ b/app/Repositories/Eloquent/EloquentRepository.php @@ -98,7 +98,7 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf $saved = $instance->skipValidation()->save(); } else { if (!$saved = $instance->save()) { - throw new DataValidationException($instance->getValidator()); + throw new DataValidationException($instance->getValidator(), $instance); } } @@ -195,7 +195,7 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf $saved = $instance->skipValidation()->save(); } else { if (!$saved = $instance->save()) { - throw new DataValidationException($instance->getValidator()); + throw new DataValidationException($instance->getValidator(), $instance); } }