From 9e44eb0d1548c4ad77f10eed2baa5fdc88af6b26 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Fri, 23 Dec 2016 02:08:22 +0100
Subject: [PATCH] Fix return code REST similar for all api with POST

---
 htdocs/adherents/class/api_members.class.php        |  4 ++--
 htdocs/adherents/class/api_subscriptions.class.php  |  4 ++--
 htdocs/categories/class/api_categories.class.php    |  4 ++--
 .../class/api_deprecated_category.class.php         |  2 +-
 htdocs/comm/action/class/api_agendaevents.class.php |  5 ++---
 htdocs/comm/propal/class/api_proposals.class.php    |  5 ++---
 htdocs/commande/class/api_orders.class.php          |  5 ++---
 htdocs/compta/bank/class/api_bankaccounts.class.php |  2 +-
 htdocs/compta/facture/class/api_invoices.class.php  |  5 ++---
 .../class/api_expensereports.class.php              |  5 ++---
 htdocs/fourn/class/api_supplier_invoices.class.php  |  5 ++---
 .../product/class/api_deprecated_product.class.php  |  2 +-
 htdocs/product/class/api_products.class.php         |  5 ++---
 htdocs/product/stock/class/api_warehouses.class.php |  4 ++--
 htdocs/projet/class/api_projects.class.php          |  5 ++---
 htdocs/projet/class/api_tasks.class.php             |  5 ++---
 htdocs/societe/class/api_contacts.class.php         |  5 ++++-
 htdocs/societe/class/api_thirdparties.class.php     |  5 ++++-
 htdocs/societe/class/societe.class.php              |  6 +++---
 htdocs/user/class/api_users.class.php               | 13 +++++--------
 20 files changed, 45 insertions(+), 51 deletions(-)

diff --git a/htdocs/adherents/class/api_members.class.php b/htdocs/adherents/class/api_members.class.php
index 779b5213666..9ea36bf579a 100644
--- a/htdocs/adherents/class/api_members.class.php
+++ b/htdocs/adherents/class/api_members.class.php
@@ -170,8 +170,8 @@ class Members extends DolibarrApi
         foreach($request_data as $field => $value) {
             $member->$field = $value;
         }
-        if($member->create(DolibarrApiAccess::$user) < 0) {
-            throw new RestException(503, 'Error when create member : '.$member->error);
+        if ($member->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors));
         }
         return $member->id;
     }
diff --git a/htdocs/adherents/class/api_subscriptions.class.php b/htdocs/adherents/class/api_subscriptions.class.php
index 1917e3b84ed..4e1d58971cb 100644
--- a/htdocs/adherents/class/api_subscriptions.class.php
+++ b/htdocs/adherents/class/api_subscriptions.class.php
@@ -162,8 +162,8 @@ class Subscriptions extends DolibarrApi
         foreach($request_data as $field => $value) {
             $subscription->$field = $value;
         }
-        if($subscription->create(DolibarrApiAccess::$user) < 0) {
-            throw new RestException(503, 'Error when create subscription : '.$subscription->error);
+        if ($subscription->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, 'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
         }
         return $subscription->id;
     }
diff --git a/htdocs/categories/class/api_categories.class.php b/htdocs/categories/class/api_categories.class.php
index 921198ab1be..4904bbea3b7 100644
--- a/htdocs/categories/class/api_categories.class.php
+++ b/htdocs/categories/class/api_categories.class.php
@@ -269,8 +269,8 @@ class Categories extends DolibarrApi
         foreach($request_data as $field => $value) {
             $this->category->$field = $value;
         }
-        if($this->category->create(DolibarrApiAccess::$user) < 0) {
-            throw new RestException(503, 'Error when create category : '.$this->category->error);
+        if ($this->category->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, 'Error when creating category', array_merge(array($this->category->error), $this->category->errors));
         }
         return $this->category->id;
     }
diff --git a/htdocs/categories/class/api_deprecated_category.class.php b/htdocs/categories/class/api_deprecated_category.class.php
index 8ec125290c5..62f92b71bea 100644
--- a/htdocs/categories/class/api_deprecated_category.class.php
+++ b/htdocs/categories/class/api_deprecated_category.class.php
@@ -395,7 +395,7 @@ class CategoryApi extends DolibarrApi
             $this->category->$field = $value;
         }
         if($this->category->create(DolibarrApiAccess::$user) < 0) {
-            throw new RestException(503, 'Error when create category : '.$this->category->error);
+            throw new RestException(500, 'Error when create category : '.$this->category->error);
         }
         return $this->category->id;
     }
diff --git a/htdocs/comm/action/class/api_agendaevents.class.php b/htdocs/comm/action/class/api_agendaevents.class.php
index 7d66dccdd63..b14d48c174e 100644
--- a/htdocs/comm/action/class/api_agendaevents.class.php
+++ b/htdocs/comm/action/class/api_agendaevents.class.php
@@ -194,9 +194,8 @@ class AgendaEvents extends DolibarrApi
           }
           $this->expensereport->lines = $lines;
         }*/
-        if ($this->actioncomm->create(DolibarrApiAccess::$user) <= 0) {
-            $errormsg = $this->actioncomm->error;
-            throw new RestException(500, $errormsg ? $errormsg : "Error while creating actioncomm");
+        if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
         }
         
         return $this->actioncomm->id;
diff --git a/htdocs/comm/propal/class/api_proposals.class.php b/htdocs/comm/propal/class/api_proposals.class.php
index 6ceac37a535..0440793ceac 100644
--- a/htdocs/comm/propal/class/api_proposals.class.php
+++ b/htdocs/comm/propal/class/api_proposals.class.php
@@ -190,9 +190,8 @@ class Proposals extends DolibarrApi
           }
           $this->propal->lines = $lines;
         }*/
-        if ($this->propal->create(DolibarrApiAccess::$user) <= 0) {
-            $errormsg = $this->propal->error;
-            throw new RestException(500, $errormsg ? $errormsg : "Error while creating order");
+        if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
         }
         
         return $this->propal->id;
diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php
index 984ef5e6f43..e0ab753c64c 100644
--- a/htdocs/commande/class/api_orders.class.php
+++ b/htdocs/commande/class/api_orders.class.php
@@ -195,9 +195,8 @@ class Orders extends DolibarrApi
           }
           $this->commande->lines = $lines;
         }*/
-        if ($this->commande->create(DolibarrApiAccess::$user) <= 0) {
-            $errormsg = $this->commande->error;
-            throw new RestException(500, $errormsg ? $errormsg : "Error while creating order");
+        if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
         }
 
         return $this->commande->id;
diff --git a/htdocs/compta/bank/class/api_bankaccounts.class.php b/htdocs/compta/bank/class/api_bankaccounts.class.php
index d6fb3515c81..7eaf673c2e7 100644
--- a/htdocs/compta/bank/class/api_bankaccounts.class.php
+++ b/htdocs/compta/bank/class/api_bankaccounts.class.php
@@ -160,7 +160,7 @@ class BankAccounts extends DolibarrApi
         $account->courant = $account->type;
 
         if ($account->create(DolibarrApiAccess::$user) < 0) {
-            throw new RestException(503, 'Error when creating account: ' . $account->error);
+            throw new RestException(500, 'Error creating bank account', array_merge(array($account->error), $account->errors));
         }
         return $account->id;
     }
diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php
index 6fc96a4dfe6..4efc997cc42 100644
--- a/htdocs/compta/facture/class/api_invoices.class.php
+++ b/htdocs/compta/facture/class/api_invoices.class.php
@@ -203,9 +203,8 @@ class Invoices extends DolibarrApi
             $this->invoice->lines = $lines;
         }*/
         
-        if ($this->invoice->create(DolibarrApiAccess::$user) <= 0) {
-            $errormsg = $this->invoice->error;
-            throw new RestException(500, $errormsg ? $errormsg : "Error while creating order");
+        if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, "Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
         }
         return $this->invoice->id;
     }
diff --git a/htdocs/expensereport/class/api_expensereports.class.php b/htdocs/expensereport/class/api_expensereports.class.php
index cb506d32cdf..94d41e432f3 100644
--- a/htdocs/expensereport/class/api_expensereports.class.php
+++ b/htdocs/expensereport/class/api_expensereports.class.php
@@ -177,9 +177,8 @@ class ExpenseReports extends DolibarrApi
           }
           $this->expensereport->lines = $lines;
         }*/
-        if ($this->expensereport->create(DolibarrApiAccess::$user) <= 0) {
-            $errormsg = $this->expensereport->error;
-            throw new RestException(500, $errormsg ? $errormsg : "Error while creating expensereport");
+        if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, "Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
         }
         
         return $this->expensereport->id;
diff --git a/htdocs/fourn/class/api_supplier_invoices.class.php b/htdocs/fourn/class/api_supplier_invoices.class.php
index cca9743927b..59e9db1324d 100644
--- a/htdocs/fourn/class/api_supplier_invoices.class.php
+++ b/htdocs/fourn/class/api_supplier_invoices.class.php
@@ -203,9 +203,8 @@ class SupplierInvoices extends DolibarrApi
             $this->invoice->lines = $lines;
         }*/
         
-        if ($this->invoice->create(DolibarrApiAccess::$user) <= 0) {
-            $errormsg = $this->invoice->error;
-            throw new RestException(500, $errormsg ? $errormsg : "Error while creating order");
+        if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, "Error creating order", array_merge(array($this->invoice->error), $this->invoice->errors));
         }
         return $this->invoice->id;
     }
diff --git a/htdocs/product/class/api_deprecated_product.class.php b/htdocs/product/class/api_deprecated_product.class.php
index 938e591226f..df0cddd8aaf 100644
--- a/htdocs/product/class/api_deprecated_product.class.php
+++ b/htdocs/product/class/api_deprecated_product.class.php
@@ -275,7 +275,7 @@ class ProductApi extends DolibarrApi
         }
         $result = $this->product->create(DolibarrApiAccess::$user);
         if($result < 0) {
-            throw new RestException(503,'Error when creating product : '.$this->product->error);
+            throw new RestException(500,'Error when creating product : '.$this->product->error);
         }
         
         return $this->product->id;
diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php
index 81b4458706c..90bc9d7296f 100644
--- a/htdocs/product/class/api_products.class.php
+++ b/htdocs/product/class/api_products.class.php
@@ -182,9 +182,8 @@ class Products extends DolibarrApi
         foreach($request_data as $field => $value) {
             $this->product->$field = $value;
         }
-        $result = $this->product->create(DolibarrApiAccess::$user);
-        if($result < 0) {
-            throw new RestException(503,'Error when creating product : '.$this->product->error);
+        if ($this->product->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, "Error creating product", array_merge(array($this->product->error), $this->product->errors));
         }
         
         return $this->product->id;
diff --git a/htdocs/product/stock/class/api_warehouses.class.php b/htdocs/product/stock/class/api_warehouses.class.php
index d19918f0789..d09c7bc527f 100644
--- a/htdocs/product/stock/class/api_warehouses.class.php
+++ b/htdocs/product/stock/class/api_warehouses.class.php
@@ -169,8 +169,8 @@ class Warehouses extends DolibarrApi
         foreach($request_data as $field => $value) {
             $this->warehouse->$field = $value;
         }
-        if($this->warehouse->create(DolibarrApiAccess::$user) <= 0) {
-            throw new RestException(503, 'Error when create warehouse : '.$this->warehouse->error);
+        if ($this->warehouse->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, "Error creating warehouse", array_merge(array($this->warehouse->error), $this->warehouse->errors));
         }
         return $this->warehouse->id;
     }
diff --git a/htdocs/projet/class/api_projects.class.php b/htdocs/projet/class/api_projects.class.php
index 78e7954a097..5bb7e8dfc7e 100644
--- a/htdocs/projet/class/api_projects.class.php
+++ b/htdocs/projet/class/api_projects.class.php
@@ -196,9 +196,8 @@ class Projects extends DolibarrApi
           }
           $this->project->lines = $lines;
         }*/
-        if ($this->project->create(DolibarrApiAccess::$user) <= 0) {
-            $errormsg = $this->project->error;
-            throw new RestException(500, $errormsg ? $errormsg : "Error while creating project");
+        if ($this->project->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, "Error creating project", array_merge(array($this->project->error), $this->project->errors));
         }
 
         return $this->project->id;
diff --git a/htdocs/projet/class/api_tasks.class.php b/htdocs/projet/class/api_tasks.class.php
index 1678e43328f..ad72a36fa45 100644
--- a/htdocs/projet/class/api_tasks.class.php
+++ b/htdocs/projet/class/api_tasks.class.php
@@ -203,9 +203,8 @@ class Tasks extends DolibarrApi
           }
           $this->project->lines = $lines;
         }*/
-        if ($this->task->create(DolibarrApiAccess::$user) <= 0) {
-            $errormsg = $this->task->error;
-            throw new RestException(500, $errormsg ? $errormsg : "Error while creating task");
+        if ($this->task->create(DolibarrApiAccess::$user) < 0) {
+            throw new RestException(500, "Error creating task", array_merge(array($this->task->error), $this->task->errors));
         }
 
         return $this->task->id;
diff --git a/htdocs/societe/class/api_contacts.class.php b/htdocs/societe/class/api_contacts.class.php
index da8e930a0ce..d758d9ac3f4 100644
--- a/htdocs/societe/class/api_contacts.class.php
+++ b/htdocs/societe/class/api_contacts.class.php
@@ -192,7 +192,10 @@ class Contacts extends DolibarrApi
 		{
 			$this->contact->$field = $value;
 		}
-		return $this->contact->create(DolibarrApiAccess::$user);
+		if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
+		    throw new RestException(500, "Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
+		}
+		return $this->contact->id;
 	}
 
 	/**
diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php
index 674d8abcf42..d8430fedb76 100644
--- a/htdocs/societe/class/api_thirdparties.class.php
+++ b/htdocs/societe/class/api_thirdparties.class.php
@@ -192,7 +192,10 @@ class Thirdparties extends DolibarrApi
       foreach($request_data as $field => $value) {
           $this->company->$field = $value;
       }
-      return $this->company->create(DolibarrApiAccess::$user);
+      if ($this->company->create(DolibarrApiAccess::$user) < 0)
+          throw new RestException(503, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
+      
+      return $this->company->id;
     }
 
     /**
diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php
index c89be8f72d3..4bff08c71dd 100644
--- a/htdocs/societe/class/societe.class.php
+++ b/htdocs/societe/class/societe.class.php
@@ -496,9 +496,9 @@ class Societe extends CommonObject
                 }
                 else
                 {
-                    dol_syslog(get_class($this)."::Create echec update ".$this->error, LOG_ERR);
+                    dol_syslog(get_class($this)."::Create echec update ".$this->error." ".join(',',$this->errors), LOG_ERR);
                     $this->db->rollback();
-                    return -3;
+                    return -4;
                 }
             }
             else
@@ -520,7 +520,7 @@ class Societe extends CommonObject
 
         }
         else
-       {
+        {
             $this->db->rollback();
             dol_syslog(get_class($this)."::Create fails verify ".join(',',$this->errors), LOG_WARNING);
             return -3;
diff --git a/htdocs/user/class/api_users.class.php b/htdocs/user/class/api_users.class.php
index 3fc44104628..a759f1dfe08 100644
--- a/htdocs/user/class/api_users.class.php
+++ b/htdocs/user/class/api_users.class.php
@@ -174,19 +174,16 @@ class Users extends DolibarrApi
 	    if (!isset($request_data["lastname"]))
 	         throw new RestException(400, "lastname field missing");*/
 	    //assign field values
-        $xxx=var_export($request_data, true);
-        dol_syslog("xxx=".$xxx);
         foreach ($request_data as $field => $value)
 	    {
 	          $this->useraccount->$field = $value;
 	    }
-	    
-        $result = $this->useraccount->create(DolibarrApiAccess::$user);
-	    if ($result <=0) {
-	         throw new RestException(500, "User not created : ".$this->useraccount->error);
+
+	    if ($this->useraccount->create(DolibarrApiAccess::$user) < 0) {
+             throw new RestException(500, 'Error creating', array_merge(array($this->useraccount->error), $this->useraccount->errors));
 	    }
-	    return array('id'=>$result);
-    }                
+	    return $this->useraccount->id;
+    }
 	
     
 	/**
-- 
GitLab