From dfaf8b552ac61f6f7671bd8b97a3d00932e55b11 Mon Sep 17 00:00:00 2001 From: FinnStutzenstein Date: Tue, 9 Apr 2019 16:15:23 +0200 Subject: [PATCH] Fixed handling of unregistered resources --- .../core/core-services/autoupdate.service.ts | 9 +++---- .../collectionStringMapper.service.ts | 24 ++++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/client/src/app/core/core-services/autoupdate.service.ts b/client/src/app/core/core-services/autoupdate.service.ts index 49ae85aaa..c771352dc 100644 --- a/client/src/app/core/core-services/autoupdate.service.ts +++ b/client/src/app/core/core-services/autoupdate.service.ts @@ -112,7 +112,11 @@ export class AutoupdateService { // Add the objects to the DataStore. for (const collection of Object.keys(autoupdate.changed)) { - await this.DS.add(this.mapObjectsToBaseModels(collection, autoupdate.changed[collection])); + if (this.modelMapper.isCollectionRegistered(collection)) { + await this.DS.add(this.mapObjectsToBaseModels(collection, autoupdate.changed[collection])); + } else { + console.error(`Unregistered collection "${collection}". Ignore it.`); + } } await this.DS.flushToStorage(autoupdate.to_change_id); @@ -130,9 +134,6 @@ export class AutoupdateService { */ private mapObjectsToBaseModels(collection: string, models: object[]): BaseModel[] { const targetClass = this.modelMapper.getModelConstructor(collection); - if (!targetClass) { - throw new Error(`Unregistered resource ${collection}`); - } return models.map(model => new targetClass(model)); } diff --git a/client/src/app/core/core-services/collectionStringMapper.service.ts b/client/src/app/core/core-services/collectionStringMapper.service.ts index b91aeaa9a..6b517c219 100644 --- a/client/src/app/core/core-services/collectionStringMapper.service.ts +++ b/client/src/app/core/core-services/collectionStringMapper.service.ts @@ -65,27 +65,39 @@ export class CollectionStringMapperService { } } + public isCollectionRegistered(collectionString: string): boolean { + return !!this.collectionStringMapping[collectionString]; + } + /** * @param obj The object to get the model constructor from. * @returns the model constructor */ - public getModelConstructor(obj: TypeIdentifier): ModelConstructor { - return this.collectionStringMapping[this.getCollectionString(obj)][0] as ModelConstructor; + public getModelConstructor(obj: TypeIdentifier): ModelConstructor | null { + if (this.isCollectionRegistered(this.getCollectionString(obj))) { + return this.collectionStringMapping[this.getCollectionString(obj)][0] as ModelConstructor; + } } /** * @param obj The object to get the view model constructor from. * @returns the view model constructor */ - public getViewModelConstructor(obj: TypeIdentifier): ViewModelConstructor { - return this.collectionStringMapping[this.getCollectionString(obj)][1] as ViewModelConstructor; + public getViewModelConstructor(obj: TypeIdentifier): ViewModelConstructor | null { + if (this.isCollectionRegistered(this.getCollectionString(obj))) { + return this.collectionStringMapping[this.getCollectionString(obj)][1] as ViewModelConstructor; + } } /** * @param obj The object to get the repository from. * @returns the repository */ - public getRepository(obj: TypeIdentifier): BaseRepository { - return this.collectionStringMapping[this.getCollectionString(obj)][2] as BaseRepository; + public getRepository( + obj: TypeIdentifier + ): BaseRepository | null { + if (this.isCollectionRegistered(this.getCollectionString(obj))) { + return this.collectionStringMapping[this.getCollectionString(obj)][2] as BaseRepository; + } } }