From 08cae4533641cf8aeb6b8aacad982a4a100acca8 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 8 Aug 2023 15:22:51 +0200 Subject: [PATCH] Imported Node: Don't break view if source config file is faulty --- library/Businessprocess/BpConfig.php | 47 +++++++++++++++++-- library/Businessprocess/ImportedNode.php | 14 ++++-- .../Renderer/TileRenderer/NodeTile.php | 14 +++--- .../Businessprocess/Renderer/TreeRenderer.php | 4 +- public/css/module.less | 7 +-- 5 files changed, 66 insertions(+), 20 deletions(-) diff --git a/library/Businessprocess/BpConfig.php b/library/Businessprocess/BpConfig.php index e99f81c..3e652e9 100644 --- a/library/Businessprocess/BpConfig.php +++ b/library/Businessprocess/BpConfig.php @@ -130,6 +130,9 @@ class BpConfig /** @var ProcessChanges */ protected $appliedChanges; + /** @var bool Whether the config is faulty */ + protected $isFaulty = false; + public function __construct() { } @@ -577,7 +580,13 @@ class BpConfig public function getImportedConfig($name) { if (! isset($this->importedConfigs[$name])) { - $import = $this->storage()->loadProcess($name); + try { + $import = $this->storage()->loadProcess($name); + } catch (Exception $e) { + $import = (new static()) + ->setName($name) + ->setFaulty(); + } if ($this->usesSoftStates()) { $import->useSoftStates(); @@ -687,9 +696,17 @@ class BpConfig { if ($this->hasBpNode($name)) { return $this->nodes[$name]; - } else { - throw new NotFoundError('Trying to access a missing business process node "%s"', $name); } + + $msg = $this->isFaulty() + ? sprintf( + t('Trying to import node "%s" from faulty config file "%s.conf"'), + $name, + $this->getName() + ) + : sprintf(t('Trying to access a missing business process node "%s"'), $name); + + throw new NotFoundError($msg); } /** @@ -1070,4 +1087,28 @@ class BpConfig return array_pad($parts, 2, null); } + + /** + * Set whether the config is faulty + * + * @param bool $isFaulty + * + * @return $this + */ + public function setFaulty(bool $isFaulty = true): self + { + $this->isFaulty = $isFaulty; + + return $this; + } + + /** + * Get whether the config is faulty + * + * @return bool + */ + public function isFaulty(): bool + { + return $this->isFaulty; + } } diff --git a/library/Businessprocess/ImportedNode.php b/library/Businessprocess/ImportedNode.php index e20b0a7..a0eb6b1 100644 --- a/library/Businessprocess/ImportedNode.php +++ b/library/Businessprocess/ImportedNode.php @@ -90,6 +90,15 @@ class ImportedNode extends BpNode return $this->childNames; } + public function isMissing() + { + if ($this->missing === null && $this->getBpConfig()->isFaulty()) { + $this->missing = true; + } + + return parent::isMissing(); + } + /** * @return BpNode */ @@ -121,10 +130,9 @@ class ImportedNode extends BpNode )); $node->setBpConfig($this->getBpConfig()); $node->setState(2); - $node->setMissing(false) + $node->setMissing() ->setDowntime(false) - ->setAck(false) - ->setAlias($e->getMessage()); + ->setAck(false); return $node; } diff --git a/library/Businessprocess/Renderer/TileRenderer/NodeTile.php b/library/Businessprocess/Renderer/TileRenderer/NodeTile.php index 1ca46c7..652ca0e 100644 --- a/library/Businessprocess/Renderer/TileRenderer/NodeTile.php +++ b/library/Businessprocess/Renderer/TileRenderer/NodeTile.php @@ -12,6 +12,7 @@ use Icinga\Web\Url; use ipl\Html\BaseHtmlElement; use ipl\Html\Html; use ipl\Web\Widget\Icon; +use ipl\Web\Widget\Link; use ipl\Web\Widget\StateBall; class NodeTile extends BaseHtmlElement @@ -102,11 +103,7 @@ class NodeTile extends BaseHtmlElement $this->add($link); } else { - $this->add(Html::tag( - 'span', - ['class' => 'missing-node-msg'], - sprintf('Trying to access a missing business process node "%s"', $node->getNodeName()) - )); + $this->add(new Link($node->getAlias(), $this->getMainNodeUrl($node)->getAbsoluteUrl())); } if ($this->renderer->rendersSubNode() @@ -210,12 +207,15 @@ class NodeTile extends BaseHtmlElement new Icon('sitemap') )); if ($node instanceof ImportedNode) { - if ($node->getBpConfig()->hasNode($node->getName())) { + $bpConfig = $node->getBpConfig(); + if ($bpConfig->isFaulty() || $bpConfig->hasNode($node->getName())) { $this->actions()->add(Html::tag( 'a', [ 'data-base-target' => '_next', - 'href' => $this->renderer->getSourceUrl($node)->getAbsoluteUrl(), + 'href' => $bpConfig->isFaulty() + ? $this->renderer->getBaseUrl()->setParam('config', $bpConfig->getName()) + : $this->renderer->getSourceUrl($node)->getAbsoluteUrl(), 'title' => mt( 'businessprocess', 'Show this process as part of its original configuration' diff --git a/library/Businessprocess/Renderer/TreeRenderer.php b/library/Businessprocess/Renderer/TreeRenderer.php index 308d628..097d148 100644 --- a/library/Businessprocess/Renderer/TreeRenderer.php +++ b/library/Businessprocess/Renderer/TreeRenderer.php @@ -234,7 +234,9 @@ class TreeRenderer extends Renderer } elseif ($differentConfig) { $summary->add($this->actionIcon( 'share', - $this->getSourceUrl($node)->addParams(['mode' => 'tree'])->getAbsoluteUrl(), + $node->getBpConfig()->isFaulty() + ? $this->getBaseUrl()->setParam('config', $node->getBpConfig()->getName()) + : $this->getSourceUrl($node)->addParams(['mode' => 'tree'])->getAbsoluteUrl(), mt('businessprocess', 'Show this process as part of its original configuration') )->addAttributes(['data-base-target' => '_next'])); } diff --git a/public/css/module.less b/public/css/module.less index 65abdfd..500a137 100644 --- a/public/css/module.less +++ b/public/css/module.less @@ -481,8 +481,7 @@ td > a > .state-badges { border: 1px solid @body-bg-color; } - > a, - .missing-node-msg { + > a { display: block; text-decoration: none; font-size: 0.7em; @@ -492,10 +491,6 @@ td > a > .state-badges { word-wrap: break-word; } - .missing-node-msg { - font-size: 0.5em; - } - &:hover { box-shadow: 0 0 .2em @gray; }