Skip to content

EspoCRM – Open Source CRM Application

before save API exception translation

Yurii355eab946919
diff --git a/application/Espo/Core/Formula/Exceptions/WrapperException.php b/application/Espo/Core/Formula/Exceptions/WrapperException.php
new file mode 100644
index 0000000000..346ec8f491
--- /dev/null
+++ b/application/Espo/Core/Formula/Exceptions/WrapperException.php
@@ -0,0 +1,60 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Formula\Exceptions;
+
+use Espo\Core\Exceptions\BadRequest;
+use Espo\Core\Exceptions\Conflict;
+use Espo\Core\Exceptions\Forbidden;
+
+/**
+ * @internal
+ */
+class WrapperException extends Error
+{
+ private Conflict|BadRequest|Forbidden $wrappedException;
+
+ private function __construct()
+ {
+ parent::__construct();
+ }
+
+ public static function create(Conflict|BadRequest|Forbidden $wrappedException): self
+ {
+ $created = new self();
+ $created->wrappedException = $wrappedException;
+
+ return $created;
+ }
+
+ public function getWrappedException(): Conflict|BadRequest|Forbidden
+ {
+ return $this->wrappedException;
+ }
+}
diff --git a/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowBadRequestType.php b/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowBadRequestType.php
index 5fe5467aea..ed5cd20ef2 100644
--- a/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowBadRequestType.php
+++ b/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowBadRequestType.php
@@ -32,18 +32,15 @@ namespace Espo\Core\Formula\Functions\RecordServiceGroup;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Error\Body;
use Espo\Core\Formula\ArgumentList;
-use Espo\Core\Formula\Exceptions\Error;
-use Espo\Core\Formula\Exceptions\ExecutionException;
+use Espo\Core\Formula\Exceptions\WrapperException;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Utils\Json;
+/**
+ * @noinspection PhpUnused
+ */
class ThrowBadRequestType extends BaseFunction
{
- /**
- * @throws BadRequest
- * @throws Error
- * @throws ExecutionException
- */
public function process(ArgumentList $args)
{
if (empty($this->getVariables()->__isRecordService)) {
@@ -54,9 +51,13 @@ class ThrowBadRequestType extends BaseFunction
$body = isset($args[1]) ? $this->evaluate($args[1]) : null;
if ($body !== null) {
- throw BadRequest::createWithBody($message, Json::encode($body));
+ throw WrapperException::create(
+ BadRequest::createWithBody($message, Json::encode($body))
+ );
}
- throw BadRequest::createWithBody($message, Body::create()->withMessage($message));
+ throw WrapperException::create(
+ BadRequest::createWithBody($message, Body::create()->withMessage($message))
+ );
}
}
diff --git a/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowConflictType.php b/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowConflictType.php
index 9b36b3beb1..52d3d31139 100644
--- a/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowConflictType.php
+++ b/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowConflictType.php
@@ -29,22 +29,18 @@
namespace Espo\Core\Formula\Functions\RecordServiceGroup;
-use Espo\Core\Exceptions\Conflict;
use Espo\Core\Exceptions\ConflictSilent;
use Espo\Core\Exceptions\Error\Body;
use Espo\Core\Formula\ArgumentList;
-use Espo\Core\Formula\Exceptions\Error;
-use Espo\Core\Formula\Exceptions\ExecutionException;
+use Espo\Core\Formula\Exceptions\WrapperException;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Utils\Json;
+/**
+ * @noinspection PhpUnused
+ */
class ThrowConflictType extends BaseFunction
{
- /**
- * @throws Error
- * @throws ExecutionException
- * @throws Conflict
- */
public function process(ArgumentList $args)
{
if (empty($this->getVariables()->__isRecordService)) {
@@ -55,9 +51,13 @@ class ThrowConflictType extends BaseFunction
$body = isset($args[1]) ? $this->evaluate($args[1]) : null;
if ($body !== null) {
- throw ConflictSilent::createWithBody($message, Json::encode($body));
+ throw WrapperException::create(
+ ConflictSilent::createWithBody($message, Json::encode($body))
+ );
}
- throw ConflictSilent::createWithBody($message, Body::create()->withMessage($message));
+ throw WrapperException::create(
+ ConflictSilent::createWithBody($message, Body::create()->withMessage($message))
+ );
}
}
diff --git a/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowDuplicateConflictType.php b/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowDuplicateConflictType.php
index 7a2137d31c..cf6ba47ec4 100644
--- a/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowDuplicateConflictType.php
+++ b/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowDuplicateConflictType.php
@@ -33,13 +33,16 @@ use Espo\Core\Di\EntityManagerAware;
use Espo\Core\Di\EntityManagerSetter;
use Espo\Core\Di\RecordServiceContainerAware;
use Espo\Core\Di\RecordServiceContainerSetter;
-use Espo\Core\Exceptions\Conflict;
use Espo\Core\Exceptions\ConflictSilent;
use Espo\Core\Formula\ArgumentList;
+use Espo\Core\Formula\Exceptions\WrapperException;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Name\Field;
use Espo\Core\Utils\Json;
+/**
+ * @noinspection PhpUnused
+ */
class ThrowDuplicateConflictType extends BaseFunction implements
EntityManagerAware,
RecordServiceContainerAware
@@ -47,10 +50,6 @@ class ThrowDuplicateConflictType extends BaseFunction implements
use EntityManagerSetter;
use RecordServiceContainerSetter;
- /**
- * @inheritDoc
- * @throws Conflict
- */
public function process(ArgumentList $args)
{
if (empty($this->getVariables()->__isRecordService)) {
@@ -90,6 +89,8 @@ class ThrowDuplicateConflictType extends BaseFunction implements
$list[] = $entity->getValueMap();
}
- throw ConflictSilent::createWithBody('duplicate', Json::encode($list));
+ throw WrapperException::create(
+ ConflictSilent::createWithBody('duplicate', Json::encode($list))
+ );
}
}
diff --git a/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowForbiddenType.php b/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowForbiddenType.php
index 145ff1cbae..ed2aa92903 100644
--- a/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowForbiddenType.php
+++ b/application/Espo/Core/Formula/Functions/RecordServiceGroup/ThrowForbiddenType.php
@@ -30,21 +30,17 @@
namespace Espo\Core\Formula\Functions\RecordServiceGroup;
use Espo\Core\Exceptions\Error\Body;
-use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\ForbiddenSilent;
use Espo\Core\Formula\ArgumentList;
-use Espo\Core\Formula\Exceptions\Error;
-use Espo\Core\Formula\Exceptions\ExecutionException;
+use Espo\Core\Formula\Exceptions\WrapperException;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Utils\Json;
+/**
+ * @noinspection PhpUnused
+ */
class ThrowForbiddenType extends BaseFunction
{
- /**
- * @throws Error
- * @throws ExecutionException
- * @throws Forbidden
- */
public function process(ArgumentList $args)
{
if (empty($this->getVariables()->__isRecordService)) {
@@ -55,9 +51,14 @@ class ThrowForbiddenType extends BaseFunction
$body = isset($args[1]) ? $this->evaluate($args[1]) : null;
if ($body !== null) {
- throw ForbiddenSilent::createWithBody($message, Json::encode($body));
+ throw WrapperException::create(
+ ForbiddenSilent::createWithBody($message, Json::encode($body))
+ );
+
}
- throw ForbiddenSilent::createWithBody($message, Body::create()->withMessage($message));
+ throw WrapperException::create(
+ ForbiddenSilent::createWithBody($message, Body::create()->withMessage($message))
+ );
}
}
diff --git a/application/Espo/Core/Record/Formula/Processor.php b/application/Espo/Core/Record/Formula/Processor.php
index 346b2a5bd7..2ffa2c9807 100644
--- a/application/Espo/Core/Record/Formula/Processor.php
+++ b/application/Espo/Core/Record/Formula/Processor.php
@@ -29,7 +29,11 @@
namespace Espo\Core\Record\Formula;
+use Espo\Core\Exceptions\BadRequest;
+use Espo\Core\Exceptions\Conflict;
+use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Formula\Exceptions\Error as FormulaError;
+use Espo\Core\Formula\Exceptions\WrapperException;
use Espo\Core\Formula\Manager as FormulaManager;
use Espo\Core\Record\CreateParams;
use Espo\Core\Record\UpdateParams;
@@ -45,11 +49,15 @@ class Processor
{
public function __construct(
private FormulaManager $formulaManager,
- private Metadata $metadata
+ private Metadata $metadata,
) {}
/**
* Process a before-create formula script.
+ *
+ * @throws BadRequest
+ * @throws Forbidden
+ * @throws Conflict
*/
public function processBeforeCreate(Entity $entity, CreateParams $params): void
{
@@ -69,6 +77,10 @@ class Processor
/**
* Process a before-update formula script.
+ *
+ * @throws BadRequest
+ * @throws Forbidden
+ * @throws Conflict
*/
public function processBeforeUpdate(Entity $entity, UpdateParams $params): void
{
@@ -86,12 +98,19 @@ class Processor
$this->run($script, $entity, $variables);
}
+ /**
+ * @throws BadRequest
+ * @throws Forbidden
+ * @throws Conflict
+ */
private function run(string $script, Entity $entity, stdClass $variables): void
{
try {
$this->formulaManager->run($script, $entity, $variables);
+ } catch (WrapperException $e) {
+ throw $e->getWrappedException();
} catch (FormulaError $e) {
- throw new RuntimeException('Formula script error.', 500, $e);
+ throw new RuntimeException('Before save API script error.', previous: $e);
}
}
diff --git a/application/Espo/Core/Record/Service.php b/application/Espo/Core/Record/Service.php
index 002d1aeaf3..a0f999351c 100644
--- a/application/Espo/Core/Record/Service.php
+++ b/application/Espo/Core/Record/Service.php
@@ -1674,6 +1674,9 @@ class Service implements Crud,
/**
* @param TEntity $entity
* @noinspection PhpDocSignatureInspection
+ * @throws Conflict
+ * @throws BadRequest
+ * @throws Forbidden
*/
private function processApiBeforeCreateApiScript(Entity $entity, CreateParams $params): void
{
@@ -1685,6 +1688,9 @@ class Service implements Crud,
/**
* @param TEntity $entity
* @noinspection PhpDocSignatureInspection
+ * @throws Conflict
+ * @throws BadRequest
+ * @throws Forbidden
*/
private function processApiBeforeUpdateApiScript(Entity $entity, UpdateParams $params): void
{