SelfService: fix error handling

fixes #1728
This commit is contained in:
Thomas Gelf 2020-11-18 19:51:40 +01:00
parent c12f5876ad
commit 5c8a12da38
2 changed files with 30 additions and 8 deletions

View File

@ -79,16 +79,22 @@ class SelfServiceController extends ActionController
} else {
$error = implode('; ', $form->getErrorMessages());
if ($error === '') {
foreach ($form->getErrors() as $elName => $errors) {
if (in_array('isEmpty', $errors)) {
if ($form->isMissingRequiredFields()) {
$fields = $form->listMissingRequiredFields();
if (count($fields) === 1) {
$this->sendPowerShellError(
sprintf("%s is required", $elName),
sprintf("%s is required", $fields[0]),
400
);
return;
} else {
$this->sendPowerShellError('An unknown error ocurred', 500);
$this->sendPowerShellError(
sprintf("Missing parameters: %s", implode(', ', $fields)),
400
);
}
return;
} else {
$this->sendPowerShellError('An unknown error ocurred', 500);
}
} else {
$this->sendPowerShellError($error, 400);
@ -171,13 +177,12 @@ class SelfServiceController extends ActionController
*/
protected function sendPowerShellError($error, $code)
{
$this->getResponse()->setHttpResponseCode($code);
if ($this->getRequest()->getHeader('X-Director-Accept') === 'text/plain') {
$this->getResponse()->setHttpResponseCode($code);
echo "ERROR: $error";
} else {
$this->sendJsonError($this->getResponse(), $error);
$this->sendJsonError($this->getResponse(), $error, $code);
}
exit;
}
/**

View File

@ -83,6 +83,23 @@ class IcingaHostSelfServiceForm extends DirectorForm
return $this->template;
}
public function listMissingRequiredFields()
{
$result = [];
foreach ($this->getElements() as $element) {
if (in_array('isEmpty', $element->getErrors())) {
$result[] = $element->getName();
}
}
return $result;
}
public function isMissingRequiredFields()
{
return count($this->listMissingRequiredFields()) > 0;
}
public function onSuccess()
{
$db = $this->getDb();