ソースを参照

fix pour la gestion des erreurs et ID manquant

Vincent 1 年間 前
コミット
34ecb2e84e

+ 1 - 4
models/Organization/DolibarrAccount.ts

@@ -1,4 +1,4 @@
-import { Attr, Num, Str, Uid } from 'pinia-orm/dist/decorators'
+import { Attr, Str, Uid } from 'pinia-orm/dist/decorators'
 import ApiResource from '~/models/ApiResource'
 
 /**
@@ -12,9 +12,6 @@ export default class DolibarrAccount extends ApiResource {
   @Uid()
   declare id: number | string | null
 
-  @Num(0, { notNullable: true })
-  declare organizationId: number
-
   @Str(null)
   declare clientNumber: string
 

+ 18 - 14
services/data/entityManager.ts

@@ -178,24 +178,28 @@ class EntityManager {
     id: number,
     forceRefresh: boolean = false,
   ): Promise<ApiResource> {
-    // If the model instance is already in the store and forceRefresh is false, return the object in store
-    if (!forceRefresh) {
-      // TODO: est-ce qu'il y a vraiment des situations où on appellera cette méthode sans le forceRefresh?
-      const item = this.find(model, id)
-      if (item && typeof item !== 'undefined') {
-        return item
+    try {
+      // If the model instance is already in the store and forceRefresh is false, return the object in store
+      if (!forceRefresh) {
+        // TODO: est-ce qu'il y a vraiment des situations où on appellera cette méthode sans le forceRefresh?
+        const item = this.find(model, id)
+        if (item && typeof item !== 'undefined') {
+          return item
+        }
       }
-    }
 
-    // Else, get the object from the API
-    const url = UrlUtils.join('api', model.entity, String(id))
-    const response = await this.apiRequestService.get(url)
+      // Else, get the object from the API
+      const url = UrlUtils.join('api', model.entity, String(id))
+      const response = await this.apiRequestService.get(url)
 
-    // deserialize the response
-    const attributes = HydraNormalizer.denormalize(response, model)
-      .data as object
+      // deserialize the response
+      const attributes = HydraNormalizer.denormalize(response, model)
+        .data as object
 
-    return this.newInstance(model, attributes)
+      return this.newInstance(model, attributes)
+    } catch (e) {
+      console.error(e)
+    }
   }
 
   /**

+ 4 - 0
services/data/normalizer/hydraNormalizer.ts

@@ -171,6 +171,10 @@ class HydraNormalizer {
   }
 
   protected static denormalizeEntity(model: typeof ApiResource, item: AnyJson) {
+    if (item.id === undefined) {
+      throw new Error('Missing id field for ' + model)
+    }
+
     // eslint-disable-next-line new-cap
     const instance = new model(item)
     const iriEncodedFields = model.getIriEncodedFields()