From 9892039b0e3b19a0d4147fc24353598a1a2831c2 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 25 Oct 2022 10:46:50 +0200 Subject: [PATCH] BasketSnapshot: show object-related details... ...in case an error occurs at encoding time fixes #2646 --- doc/82-Changelog.md | 3 ++ .../Automation/BasketSnapshot.php | 31 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index 13d615be..08cba0ca 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -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) diff --git a/library/Director/DirectorObject/Automation/BasketSnapshot.php b/library/Director/DirectorObject/Automation/BasketSnapshot.php index 5dcb2d0e..96988173 100644 --- a/library/Director/DirectorObject/Automation/BasketSnapshot.php +++ b/library/Director/DirectorObject/Automation/BasketSnapshot.php @@ -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)