Skip to content

Commit 43156e8

Browse files
committed
Improve error messaging for validation exceptions
1 parent 271197e commit 43156e8

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

app/Exceptions/Model/DataValidationException.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pterodactyl\Exceptions\Model;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Contracts\Validation\Validator;
67
use Pterodactyl\Exceptions\PterodactylException;
78
use Illuminate\Contracts\Support\MessageProvider;
@@ -11,21 +12,30 @@ class DataValidationException extends PterodactylException implements HttpExcept
1112
{
1213
/**
1314
* The validator instance.
14-
*
15-
* @var \Illuminate\Contracts\Validation\Validator
1615
*/
17-
public $validator;
16+
protected Validator $validator;
17+
18+
/**
19+
* The underlying model instance that triggered this exception.
20+
*/
21+
protected Model $model;
1822

1923
/**
2024
* DataValidationException constructor.
2125
*/
22-
public function __construct(Validator $validator)
26+
public function __construct(Validator $validator, Model $model)
2327
{
24-
parent::__construct(
25-
'Data integrity exception encountered while performing database write operation. ' . $validator->errors()->toJson()
28+
$message = sprintf(
29+
'Could not save %s[%s]: failed to validate data: %s',
30+
get_class($model),
31+
$model->getKey(),
32+
$validator->errors()->toJson()
2633
);
2734

35+
parent::__construct($message);
36+
2837
$this->validator = $validator;
38+
$this->model = $model;
2939
}
3040

3141
/**
@@ -55,4 +65,14 @@ public function getHeaders()
5565
{
5666
return [];
5767
}
68+
69+
public function getValidator(): Validator
70+
{
71+
return $this->validator;
72+
}
73+
74+
public function getModel(): Model
75+
{
76+
return $this->model;
77+
}
5878
}

app/Http/Controllers/Admin/ServersController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public function updateBuild(Request $request, Server $server)
271271
'database_limit', 'allocation_limit', 'backup_limit', 'oom_disabled',
272272
]));
273273
} catch (DataValidationException $exception) {
274-
throw new ValidationException($exception->validator);
274+
throw new ValidationException($exception->getValidator());
275275
}
276276

277277
$this->alert->success(trans('admin/server.alerts.build_updated'))->flash();
@@ -315,7 +315,7 @@ public function saveStartup(Request $request, Server $server)
315315
->setUserLevel(User::USER_LEVEL_ADMIN)
316316
->handle($server, $data);
317317
} catch (DataValidationException $exception) {
318-
throw new ValidationException($exception->validator);
318+
throw new ValidationException($exception->getValidator());
319319
}
320320

321321
$this->alert->success(trans('admin/server.alerts.startup_changed'))->flash();

app/Models/Model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected static function boot()
5757
try {
5858
$model->validate();
5959
} catch (ValidationException $exception) {
60-
throw new DataValidationException($exception->validator);
60+
throw new DataValidationException($exception->validator, $model);
6161
}
6262

6363
return true;

app/Repositories/Eloquent/EloquentRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function create(array $fields, bool $validate = true, bool $force = false
9898
$saved = $instance->skipValidation()->save();
9999
} else {
100100
if (!$saved = $instance->save()) {
101-
throw new DataValidationException($instance->getValidator());
101+
throw new DataValidationException($instance->getValidator(), $instance);
102102
}
103103
}
104104

@@ -195,7 +195,7 @@ public function update($id, array $fields, bool $validate = true, bool $force =
195195
$saved = $instance->skipValidation()->save();
196196
} else {
197197
if (!$saved = $instance->save()) {
198-
throw new DataValidationException($instance->getValidator());
198+
throw new DataValidationException($instance->getValidator(), $instance);
199199
}
200200
}
201201

0 commit comments

Comments
 (0)