Improve error messaging for validation exceptions

This commit is contained in:
DaneEveritt 2022-06-26 16:31:48 -04:00
parent 271197e823
commit 43156e8d53
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 31 additions and 11 deletions

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Exceptions\Model; namespace Pterodactyl\Exceptions\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Validation\Validator; use Illuminate\Contracts\Validation\Validator;
use Pterodactyl\Exceptions\PterodactylException; use Pterodactyl\Exceptions\PterodactylException;
use Illuminate\Contracts\Support\MessageProvider; use Illuminate\Contracts\Support\MessageProvider;
@ -11,21 +12,30 @@ class DataValidationException extends PterodactylException implements HttpExcept
{ {
/** /**
* The validator instance. * 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. * DataValidationException constructor.
*/ */
public function __construct(Validator $validator) public function __construct(Validator $validator, Model $model)
{ {
parent::__construct( $message = sprintf(
'Data integrity exception encountered while performing database write operation. ' . $validator->errors()->toJson() '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->validator = $validator;
$this->model = $model;
} }
/** /**
@ -55,4 +65,14 @@ class DataValidationException extends PterodactylException implements HttpExcept
{ {
return []; return [];
} }
public function getValidator(): Validator
{
return $this->validator;
}
public function getModel(): Model
{
return $this->model;
}
} }

View file

@ -271,7 +271,7 @@ class ServersController extends Controller
'database_limit', 'allocation_limit', 'backup_limit', 'oom_disabled', 'database_limit', 'allocation_limit', 'backup_limit', 'oom_disabled',
])); ]));
} catch (DataValidationException $exception) { } catch (DataValidationException $exception) {
throw new ValidationException($exception->validator); throw new ValidationException($exception->getValidator());
} }
$this->alert->success(trans('admin/server.alerts.build_updated'))->flash(); $this->alert->success(trans('admin/server.alerts.build_updated'))->flash();
@ -315,7 +315,7 @@ class ServersController extends Controller
->setUserLevel(User::USER_LEVEL_ADMIN) ->setUserLevel(User::USER_LEVEL_ADMIN)
->handle($server, $data); ->handle($server, $data);
} catch (DataValidationException $exception) { } catch (DataValidationException $exception) {
throw new ValidationException($exception->validator); throw new ValidationException($exception->getValidator());
} }
$this->alert->success(trans('admin/server.alerts.startup_changed'))->flash(); $this->alert->success(trans('admin/server.alerts.startup_changed'))->flash();

View file

@ -57,7 +57,7 @@ abstract class Model extends IlluminateModel
try { try {
$model->validate(); $model->validate();
} catch (ValidationException $exception) { } catch (ValidationException $exception) {
throw new DataValidationException($exception->validator); throw new DataValidationException($exception->validator, $model);
} }
return true; return true;

View file

@ -98,7 +98,7 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
$saved = $instance->skipValidation()->save(); $saved = $instance->skipValidation()->save();
} else { } else {
if (!$saved = $instance->save()) { 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(); $saved = $instance->skipValidation()->save();
} else { } else {
if (!$saved = $instance->save()) { if (!$saved = $instance->save()) {
throw new DataValidationException($instance->getValidator()); throw new DataValidationException($instance->getValidator(), $instance);
} }
} }