DirectorObjectForm: improve error handling

Template resolving has been changed, adjust error handling accordingly
This commit is contained in:
Thomas Gelf 2017-08-22 10:13:37 +02:00
parent f4cd0d7b3c
commit ab69e1c55f
1 changed files with 63 additions and 21 deletions

View File

@ -177,16 +177,22 @@ abstract class DirectorObjectForm extends DirectorForm
/** @var ZfElement $el */ /** @var ZfElement $el */
$this->populate([$key => $imports]); $this->populate([$key => $imports]);
$el->setValue($imports); $el->setValue($imports);
$this->tryToSetObjectPropertyFromElement($object, $el, $key); if (! $this->tryToSetObjectPropertyFromElement($object, $el, $key)) {
return $this->resolvedImports = false;
}
} }
} elseif ($this->presetImports) { } elseif ($this->presetImports) {
$imports = array_values(array_merge( $imports = array_values(array_merge(
$this->presetImports, $this->presetImports,
$this->extractChoicesFromPost($post) $this->extractChoicesFromPost($post)
)); ));
$this->object()->set('imports', $imports); if (! $this->eventuallySetImports($imports)) {
return $this->resolvedImports = false;
}
} else { } else {
$this->object()->set('imports', $this->extractChoicesFromPost($post)); if (! $this->eventuallySetImports($this->extractChoicesFromPost($post))) {
return $this->resolvedImports = false;
}
} }
foreach ($this->earlyProperties as $key) { foreach ($this->earlyProperties as $key) {
@ -212,6 +218,17 @@ abstract class DirectorObjectForm extends DirectorForm
return $this->setResolvedImports(); return $this->setResolvedImports();
} }
protected function eventuallySetImports($imports)
{
try {
$this->object()->set('imports', $imports);
return true;
} catch (Exception $e) {
$this->addException($e, 'imports');
return false;
}
}
protected function tryToSetObjectPropertyFromElement( protected function tryToSetObjectPropertyFromElement(
IcingaObject $object, IcingaObject $object,
ZfElement $element, ZfElement $element,
@ -222,11 +239,13 @@ abstract class DirectorObjectForm extends DirectorForm
$old = $object->get($key); $old = $object->get($key);
$object->set($key, $element->getValue()); $object->set($key, $element->getValue());
$object->resolveUnresolvedRelatedProperties(); $object->resolveUnresolvedRelatedProperties();
return true;
} catch (Exception $e) { } catch (Exception $e) {
if ($old !== null) { if ($old !== null) {
$object->set($key, $old); $object->set($key, $old);
} }
$this->addException($e, $key); $this->addException($e, $key);
return false;
} }
} }
@ -348,7 +367,9 @@ abstract class DirectorObjectForm extends DirectorForm
} }
$object->set($key, $value); $object->set($key, $value);
if ($object instanceof IcingaObject) { if ($object instanceof IcingaObject) {
$object->resolveUnresolvedRelatedProperties(); if ($this->resolvedImports !== false) {
$object->resolveUnresolvedRelatedProperties();
}
} }
} catch (Exception $e) { } catch (Exception $e) {
$this->addException($e, $key); $this->addException($e, $key);
@ -570,23 +591,29 @@ abstract class DirectorObjectForm extends DirectorForm
public function onSuccess() public function onSuccess()
{ {
$object = $this->object(); $object = $this->object();
if ($object->hasBeenModified()) {
if (! $object->hasBeenLoadedFromDb()) {
$this->setHttpResponseCode(201);
}
$msg = sprintf( try {
$object->hasBeenLoadedFromDb() if ($object->hasBeenModified()) {
? $this->translate('The %s has successfully been stored') if (! $object->hasBeenLoadedFromDb()) {
: $this->translate('A new %s has successfully been created'), $this->setHttpResponseCode(201);
$this->translate($this->getObjectShortClassName()) }
);
$object->store($this->db); $msg = sprintf(
} else { $object->hasBeenLoadedFromDb()
if ($this->isApiRequest()) { ? $this->translate('The %s has successfully been stored')
$this->setHttpResponseCode(304); : $this->translate('A new %s has successfully been created'),
$this->translate($this->getObjectShortClassName())
);
$object->store($this->db);
} else {
if ($this->isApiRequest()) {
$this->setHttpResponseCode(304);
}
$msg = $this->translate('No action taken, object has not been modified');
} }
$msg = $this->translate('No action taken, object has not been modified'); } catch (Exception $e) {
$this->addException($e);
return false;
} }
$this->setObjectSuccessUrl(); $this->setObjectSuccessUrl();
@ -716,8 +743,12 @@ abstract class DirectorObjectForm extends DirectorForm
if ($this->hasBeenSent()) { if ($this->hasBeenSent()) {
$this->handlePost(); $this->handlePost();
} }
$this->loadInheritedProperties(); try {
$this->addFields(); $this->loadInheritedProperties();
$this->addFields();
} catch (Exception $e) {
$this->addUniqueException($e);
}
} }
protected function handlePost() protected function handlePost()
@ -922,6 +953,17 @@ abstract class DirectorObjectForm extends DirectorForm
return $this; return $this;
} }
protected function addUniqueException(Exception $e)
{
$msg = $this->getErrorMessageForException($e);
if (! in_array($msg, $this->getErrorMessages())) {
$this->addErrorMessage($msg);
}
return $this;
}
public function loadObject($id) public function loadObject($id)
{ {
/** @var DbObject $class */ /** @var DbObject $class */