Use DB transaction when duplicating Segment

[MAILPOET-3170]
This commit is contained in:
Jan Lysý
2021-01-25 15:02:24 +01:00
committed by Veljko V
parent f5987359c7
commit c579a35980
2 changed files with 34 additions and 17 deletions

View File

@ -2,9 +2,11 @@
namespace MailPoet\API\JSON\v1;
use Exception;
use InvalidArgumentException;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;
use MailPoet\API\JSON\Response;
use MailPoet\API\JSON\ResponseBuilders\SegmentsResponseBuilder;
use MailPoet\Config\AccessControl;
use MailPoet\Doctrine\Validator\ValidationException;
@ -188,7 +190,13 @@ class Segments extends APIEndpoint {
$segment = $this->getSegment($data);
if ($segment instanceof SegmentEntity) {
$duplicate = $this->segmentSavecontroller->duplicate($segment);
try {
$duplicate = $this->segmentSavecontroller->duplicate($segment);
} catch (Exception $e) {
return $this->errorResponse([
APIError::UNKNOWN => __('Duplicating of segment failed.', 'mailpoet'),
], [], Response::STATUS_UNKNOWN);
}
return $this->successResponse(
$this->segmentsResponseBuilder->build($duplicate),
['count' => 1]