Commit 8906117e authored by joachim's avatar joachim Committed by fago

Issue #1621226 by joachim, DuaelFr, cthiebault, fago: Entity Metadata Wrapper:...

Issue #1621226 by joachim, DuaelFr, cthiebault, fago: Entity Metadata Wrapper: Add field information to exception message on validation exception
parent 604072f0
......@@ -119,7 +119,11 @@ abstract class EntityMetadataWrapper {
*/
public function set($value) {
if (!$this->validate($value)) {
throw new EntityMetadataWrapperException('Invalid data value given. Be sure it matches the required data type and format.');
throw new EntityMetadataWrapperException(t('Invalid data value given. Be sure it matches the required data type and format. Value at !location: !value.', array(
// An exception's message is output through check_plain().
'!value' => is_array($value) || is_object($value) ? var_export($value) : $value,
'!location' => $this->debugIdentifierLocation(),
)));
}
$this->clear();
$this->data = $value;
......@@ -231,6 +235,21 @@ abstract class EntityMetadataWrapper {
return !empty($this->info['parent']) ? $this->info['parent']->propertyAccess($this->info['name'], $op, $account) : TRUE;
}
/**
* Returns a string to use to identify this wrapper in error messages.
*
* @return
* A string that identifies this wrapper and its chain of ancestors, of the
* form 'grandparentidentifier->parentidentifier->identifier'.
*/
public function debugIdentifierLocation() {
$debug = $this->info['name'];
if (isset($this->info['parent'])) {
$debug = $this->info['parent']->debugIdentifierLocation() . '->' . $debug;
}
return $debug;
}
/**
* Prepare for serializiation.
*/
......@@ -734,7 +753,11 @@ class EntityDrupalWrapper extends EntityStructureWrapper {
*/
public function set($value) {
if (!$this->validate($value)) {
throw new EntityMetadataWrapperException('Invalid data value given. Be sure it matches the required data type and format.');
throw new EntityMetadataWrapperException(t('Invalid data value given. Be sure it matches the required data type and format. Value at !location: !value.', array(
// An exception's message is output through check_plain().
'!value' => is_array($value) || is_object($value) ? var_export($value) : $value,
'!location' => $this->debugIdentifierLocation(),
)));
}
if ($this->info['type'] == 'entity' && $value === $this) {
// Nothing to do.
......@@ -909,6 +932,27 @@ class EntityDrupalWrapper extends EntityStructureWrapper {
}
}
/**
* Returns a string to use to identify this wrapper in error messages.
*/
public function debugIdentifierLocation() {
// An entity wrapper can be at the top of the chain or a part of it.
if (isset($this->info['name'])) {
// This wrapper is part of a chain, eg in the position node->author.
// Return the name.
$debug = $this->info['name'];
}
else {
// This is a wrapper for an actual entity: return its type and id.
$debug = $this->info['type'] . '(' . $this->getIdentifier() . ')';
}
if (isset($this->info['parent'])) {
$debug = $this->info['parent']->debugIdentifierLocation() . '->' . $debug;
}
return $debug;
}
/**
* Prepare for serializiation.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment