From 759b1825fe03097428052980d98da11f061e81f1 Mon Sep 17 00:00:00 2001 From: Molkobain Date: Tue, 28 Feb 2023 13:33:47 +0100 Subject: [PATCH 1/2] =?UTF-8?q?N=C2=B05918=20-=20Fix=20activity=20panel=20?= =?UTF-8?q?disappearing=20when=20DoCheckToWrite=20fails?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/dbobject.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/dbobject.class.php b/core/dbobject.class.php index cb444e50b..59049c7c2 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -2792,12 +2792,6 @@ abstract class DBObject implements iDisplay $this->DoComputeValues(); $this->OnInsert(); - if ($this->m_iKey < 0) - { - // This was a temporary "memory" key: discard it so that DBInsertSingleTable will not try to use it! - $this->m_iKey = null; - } - // If not automatically computed, then check that the key is given by the caller if (!MetaModel::IsAutoIncrementKey($sRootClass)) { @@ -2814,6 +2808,12 @@ abstract class DBObject implements iDisplay throw new CoreCannotSaveObjectException(array('issues' => $aIssues, 'class' => get_class($this), 'id' => $this->GetKey())); } + if ($this->m_iKey < 0) + { + // This was a temporary "memory" key: discard it so that DBInsertSingleTable will not try to use it! + $this->m_iKey = null; + } + // Stop watches $sState = $this->GetState(); if ($sState != '') From 4cea418517b41741c480d5a8a0eccaba3400cbea Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Tue, 28 Feb 2023 15:13:28 +0100 Subject: [PATCH 2/2] =?UTF-8?q?N=C2=B05893=20-=20Log=20triggers=20exceptio?= =?UTF-8?q?n=20in=20CRUD=20stack=20(#390)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Log TriggerOnObjectCreate crash * Log TriggerOnObjectUpdate crash * Log TriggerOnObjectDelete crash * Factorize TriggerOnObject log * \TriggerOnObject::LogException : do not replace not persisted yet object keys --- core/dbobject.class.php | 12 ++++++++---- core/trigger.class.inc.php | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/core/dbobject.class.php b/core/dbobject.class.php index dc2fd1484..2baf3c3f7 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -2826,13 +2826,14 @@ abstract class DBObject implements iDisplay $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnObjectCreate AS t WHERE t.target_class IN (:class_list)"), array(), $aParams); while ($oTrigger = $oSet->Fetch()) { - /** @var \Trigger $oTrigger */ + /** @var \TriggerOnObjectCreate $oTrigger */ try { $oTrigger->DoActivate($this->ToArgs('this')); } catch(Exception $e) { + $oTrigger->LogException($e, $this); utils::EnrichRaisedException($oTrigger, $e); } } @@ -3150,6 +3151,7 @@ abstract class DBObject implements iDisplay $oTrigger->DoActivate($this->ToArgs('this')); } catch (Exception $e) { + $oTrigger->LogException($e, $this); utils::EnrichRaisedException($oTrigger, $e); } } @@ -3470,13 +3472,13 @@ abstract class DBObject implements iDisplay $aParams); while ($oTrigger = $oSet->Fetch()) { - /** @var \Trigger $oTrigger */ + /** @var \TriggerOnObjectDelete $oTrigger */ try { $oTrigger->DoActivate($this->ToArgs('this')); } - catch(Exception $e) - { + catch(Exception $e) { + $oTrigger->LogException($e, $this); utils::EnrichRaisedException($oTrigger, $e); } } @@ -3891,6 +3893,7 @@ abstract class DBObject implements iDisplay $oTrigger->DoActivate($this->ToArgs('this')); } catch (Exception $e) { + $oTrigger->LogException($e, $this); utils::EnrichRaisedException($oTrigger, $e); } } @@ -3902,6 +3905,7 @@ abstract class DBObject implements iDisplay $oTrigger->DoActivate($this->ToArgs('this')); } catch (Exception $e) { + $oTrigger->LogException($e, $this); utils::EnrichRaisedException($oTrigger, $e); } } diff --git a/core/trigger.class.inc.php b/core/trigger.class.inc.php index 39e7f7143..6783882ca 100644 --- a/core/trigger.class.inc.php +++ b/core/trigger.class.inc.php @@ -259,21 +259,48 @@ abstract class TriggerOnObject extends Trigger public function IsTargetObject($iObjectId, $aChanges = array()) { $sFilter = trim($this->Get('filter')); - if (strlen($sFilter) > 0) - { + if (strlen($sFilter) > 0) { $oSearch = DBObjectSearch::FromOQL($sFilter); $oSearch->AddCondition('id', $iObjectId, '='); $oSearch->AllowAllData(); $oSet = new DBObjectSet($oSearch); $bRet = ($oSet->Count() > 0); - } - else - { + } else { $bRet = true; } return $bRet; } + + /** + * @param Exception $oException + * @param \DBObject $oObject + * + * @return void + * + * @uses \IssueLog::Error() + * + * @since 2.7.9 3.0.3 3.1.0 N°5893 + */ + public function LogException($oException, $oObject) + { + $sObjectKey = $oObject->GetKey(); // if object wasn't persisted yet, then we'll have a negative value + + $aContext = [ + 'exception.class' => get_class($oException), + 'exception.message' => $oException->getMessage(), + 'trigger.class' => get_class($this), + 'trigger.id' => $this->GetKey(), + 'trigger.friendlyname' => $this->GetRawName(), + 'object.class' => get_class($oObject), + 'object.id' => $sObjectKey, + 'object.friendlyname' => $oObject->GetRawName(), + 'current_user' => UserRights::GetUser(), + 'exception.stack' => $oException->getTraceAsString(), + ]; + + IssueLog::Error('A trigger did throw an exception', null, $aContext); + } } /**