Selaa lähdekoodia

improve dolibarr service error management

Olivier Massot 3 vuotta sitten
vanhempi
commit
690b90ce88

+ 0 - 4
.env

@@ -95,7 +95,3 @@ DATABASE_AUDIT_URL=mysql://root:mysql660@db:3306/audit?serverVersion=5.7
 ###> typo3 client ###
 TYPO3_BASE_URI=http://docker.sub.opentalent.fr
 ###< typo3 client ###
-
-###> dolibarr client ###
-DOLIBARR_API_BASE_URI=https://dev-erp.2iopenservice.com/api/index.php/
-###< dolibarr client ###

+ 11 - 3
src/Service/ApiResourceBuilder/Dolibarr/DolibarrAccountBuilder.php

@@ -30,6 +30,10 @@ class DolibarrAccountBuilder
         $accountData = $this->dolibarrApiService->getSociety($id);
         $dolibarrAccount = $this->createDolibarrAccount($id, $accountData);
 
+        if ($dolibarrAccount->getSocId() === null) {
+            return $dolibarrAccount;
+        }
+
         // Get active contract and services
         $contractData = $this->dolibarrApiService->getActiveContract($dolibarrAccount->getSocId());
 
@@ -48,10 +52,14 @@ class DolibarrAccountBuilder
         return $dolibarrAccount;
     }
 
-    public function createDolibarrAccount(int $organizationId, array $accountData): DolibarrAccount {
+    public function createDolibarrAccount(int $organizationId, ?array $accountData): DolibarrAccount {
         $dolibarrAccount = new DolibarrAccount();
-        $dolibarrAccount->setOrganizationId($organizationId)
-                        ->setSocId((int)$accountData['id'])
+        $dolibarrAccount->setOrganizationId($organizationId);
+        if ($accountData === null) {
+            return $dolibarrAccount;
+        }
+
+        $dolibarrAccount->setSocId((int)$accountData['id'])
                         ->setClientNumber($accountData['code_client']);
 
         if (

+ 12 - 4
src/Service/Dolibarr/DolibarrApiService.php

@@ -26,13 +26,21 @@ class DolibarrApiService extends ApiRequestService
      * Get a dolibarr society by its opentalent organization id
      *
      * @param int $organizationId
-     * @return array
-     * @throws HttpException
+     * @return array|null
+     * @throws \JsonException
      */
-    public function getSociety(int $organizationId): array {
+    public function getSociety(int $organizationId): ?array {
         // impossible to retrieve a society by its extrafield 2iopen_organization_id (thanks dolibarr), so
         // we need to store the organization id in two fields: 2iopen_organization_id and ref_int :(
-        return $this->getJsonContent("thirdparties" , [ "limit" => "1", "sqlfilters" => "ref_int=" . $organizationId])[0];
+        try {
+            return $this->getJsonContent("thirdparties" , [ "limit" => "1", "sqlfilters" => "ref_int=" . $organizationId])[0];
+        } catch (HttpException $e) {
+            if ($e->getStatusCode() === 404) {
+                // /!\ The dolibarr API will return a 404 error if no results are found...
+                return null;
+            }
+            throw $e;
+        }
     }
 
     /**

+ 2 - 2
src/Service/Rest/ApiRequestService.php

@@ -48,7 +48,7 @@ class ApiRequestService implements ApiRequestInterface
         try {
             return $this->get($path, $parameters, $options)->getContent();
         } catch (ClientExceptionInterface | TransportExceptionInterface | RedirectionExceptionInterface | ServerExceptionInterface $e) {
-            throw new HttpException(500, 'Request error : ' . $e->getMessage(), $e);
+            throw new HttpException($e->getCode(), 'Request error : ' . $e->getMessage(), $e);
         }
     }
 
@@ -130,7 +130,7 @@ class ApiRequestService implements ApiRequestInterface
         try {
             return $this->client->request($method, $url, $options);
         } catch (TransportExceptionInterface $e) {
-            throw new HttpException(500, 'Request error : ' . $e->getMessage(), $e);
+            throw new HttpException($e->getCode(), 'Request error : ' . $e->getMessage(), $e);
         }
     }
 }