Skip to content

Commit 69c2e89

Browse files
committed
Fix some missing exceptions and validation handling for users
1 parent e2d5145 commit 69c2e89

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

app/Http/Controllers/API/UserController.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
use Pterodactyl\Models;
1010
use Pterodactyl\Transformers\UserTransformer;
1111
use Pterodactyl\Repositories\UserRepository;
12+
1213
use Pterodactyl\Exceptions\DisplayValidationException;
1314
use Pterodactyl\Exceptions\DisplayException;
15+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1417

1518
/**
1619
* @Resource("Users")
@@ -61,7 +64,17 @@ public function getUser(Request $request, $id, $fields = null)
6164
}
6265
}
6366

64-
return $query->first();
67+
try {
68+
if (!$query->first()) {
69+
throw new NotFoundHttpException('No user by that ID was found.');
70+
}
71+
return $query->first();
72+
} catch (NotFoundHttpException $ex) {
73+
throw $ex;
74+
} catch (\Exception $ex) {
75+
throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
76+
}
77+
6578
}
6679

6780
/**

app/Repositories/UserRepository.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,17 @@ public function update($id, array $data)
8080
'totp_secret' => 'size:16'
8181
]);
8282

83+
// Run validator, throw catchable and displayable exception if it fails.
84+
// Exception includes a JSON result of failed validation rules.
85+
if ($validator->fails()) {
86+
throw new DisplayValidationException($validator->errors());
87+
}
88+
8389
if(array_key_exists('password', $data)) {
8490
$user['password'] = Hash::make($data['password']);
8591
}
8692

87-
return Models\User::find($id)->update($data);
93+
return Models\User::findOrFail($id)->update($data);
8894
}
8995

9096
/**

0 commit comments

Comments
 (0)