From 0165c6d49053cd8a60008ab9b46a09433df36ee9 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 26 Nov 2022 00:22:01 +0100 Subject: [PATCH] Better error management on API delete --- htdocs/core/class/commonobject.class.php | 2 +- htdocs/modulebuilder/template/class/api_mymodule.class.php | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 5b0aebac0f0..dd961d289e2 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -9489,7 +9489,7 @@ abstract class CommonObject * @param User $user User that deletes * @param bool $notrigger false=launch triggers after, true=disable triggers * @param int $forcechilddeletion 0=no, 1=Force deletion of children - * @return int <=0 if KO, 0=Nothing done because object has child, >0 if OK + * @return int <0 if KO, 0=Nothing done because object has child, >0 if OK */ public function deleteCommon(User $user, $notrigger = false, $forcechilddeletion = 0) { diff --git a/htdocs/modulebuilder/template/class/api_mymodule.class.php b/htdocs/modulebuilder/template/class/api_mymodule.class.php index 736fd9ddc38..ce92ef5ea8e 100644 --- a/htdocs/modulebuilder/template/class/api_mymodule.class.php +++ b/htdocs/modulebuilder/template/class/api_mymodule.class.php @@ -293,7 +293,9 @@ class MyModuleApi extends DolibarrApi throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login); } - if (!$this->myobject->delete(DolibarrApiAccess::$user)) { + if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) { + throw new RestException(409, 'Error when deleting MyObject : '.$this->myobject->error); + } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) { throw new RestException(500, 'Error when deleting MyObject : '.$this->myobject->error); }