BasketSnapshot: show object-related details...

...in case an error occurs at encoding time

fixes #2646
This commit is contained in:
Thomas Gelf 2022-10-25 10:46:50 +02:00
parent bfda96f569
commit 9892039b0e
2 changed files with 33 additions and 1 deletions

View File

@ -14,6 +14,9 @@ v1.10.2 (unreleased)
### Import and Sync
* FIX: triggering Sync manually produced an error on PostgreSQL (#2636)
### Configuration Baskets
* FEATURE: more details shown in error messages related to invalid characters (#2646)
### Internals
* FIX: issue with empty activity log, deprecate outdated method (#2630)

View File

@ -2,6 +2,8 @@
namespace Icinga\Module\Director\DirectorObject\Automation;
use gipfl\Json\JsonEncodeException;
use gipfl\Json\JsonString;
use Icinga\Module\Director\Core\Json;
use Icinga\Module\Director\Data\Exporter;
use Icinga\Module\Director\Db;
@ -418,7 +420,34 @@ class BasketSnapshot extends DbObject
return $this->getContent()->get('content');
}
return Json::encode($this->objects, JSON_PRETTY_PRINT);
try {
return JsonString::encode($this->objects, JSON_PRETTY_PRINT);
} catch (JsonEncodeException $e) {
foreach ($this->objects as $type => $objects) {
foreach ($objects as $key => $object) {
try {
JsonString::encode($object);
} catch (JsonEncodeException $singleError) {
if ($object instanceof IcingaObject) {
$name = $object->getObjectName();
} else {
$name = var_export($object, 1);
if (function_exists('iconv')) {
$name = iconv('UTF-8', 'UTF-8//IGNORE', $name);
}
}
throw new JsonEncodeException(sprintf(
'Failed to encode object ot type "%s": "%s", %s',
$type,
$name,
$singleError->getMessage()
), $singleError->getCode());
}
}
}
throw $e;
}
}
protected function addAll($typeName)