Skip to content

Commit

Permalink
fix: catch errors when updating the mappings on startup (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuruyia committed Apr 9, 2024
1 parent a196e32 commit e09c5a3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
85 changes: 60 additions & 25 deletions lib/modules/plugin/DeviceManagerEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,18 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {
engineGroup,
);

await this.sdk.collection.create(
engineId,
InternalCollection.ASSETS,
mappings,
);
try {
await this.sdk.collection.create(
engineId,
InternalCollection.ASSETS,
mappings,
);
} catch (error) {
throw new InternalError(
`Failed to create the assets collection "${InternalCollection.ASSETS}" for engine "${engineId}": ${error.message}`,
error,
);
}

return InternalCollection.ASSETS;
}
Expand All @@ -182,21 +189,35 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {

_.merge(mappings.properties.asset, assetsCollectionMappings);

await this.sdk.collection.create(
engineId,
InternalCollection.ASSETS_HISTORY,
mappings,
);
try {
await this.sdk.collection.create(
engineId,
InternalCollection.ASSETS_HISTORY,
mappings,
);
} catch (error) {
throw new InternalError(
`Failed to create the assets history collection "${InternalCollection.ASSETS_HISTORY}" for engine "${engineId}": ${error.message}`,
error,
);
}

return InternalCollection.ASSETS_HISTORY;
}

async createAssetsGroupsCollection(engineId: string) {
await this.sdk.collection.create(
engineId,
InternalCollection.ASSETS_GROUPS,
{ mappings: assetGroupsMappings },
);
try {
await this.sdk.collection.create(
engineId,
InternalCollection.ASSETS_GROUPS,
{ mappings: assetGroupsMappings },
);
} catch (error) {
throw new InternalError(
`Failed to create the assets groups collection "${InternalCollection.ASSETS_GROUPS}" for engine "${engineId}": ${error.message}`,
error,
);
}

return InternalCollection.ASSETS_GROUPS;
}
Expand Down Expand Up @@ -260,23 +281,37 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {
const mappings =
await this.getDigitalTwinMappings<DeviceModelContent>("device");

await this.sdk.collection.create(
engineId,
InternalCollection.DEVICES,
mappings,
);
try {
await this.sdk.collection.create(
engineId,
InternalCollection.DEVICES,
mappings,
);
} catch (error) {
throw new InternalError(
`Failed to create the devices collection "${InternalCollection.DEVICES}" for engine "${engineId}": ${error.message}`,
error,
);
}

return InternalCollection.DEVICES;
}

async createMeasuresCollection(engineId: string, engineGroup: string) {
const mappings = await this.getMeasuresMappings(engineGroup);

await this.sdk.collection.create(
engineId,
InternalCollection.MEASURES,
mappings,
);
try {
await this.sdk.collection.create(
engineId,
InternalCollection.MEASURES,
mappings,
);
} catch (error) {
throw new InternalError(
`Failed to create the measures collection "${InternalCollection.MEASURES}" for engine "${engineId}": ${error.message}`,
error,
);
}

return InternalCollection.MEASURES;
}
Expand Down
8 changes: 7 additions & 1 deletion lib/modules/plugin/DeviceManagerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,13 @@ export class DeviceManagerPlugin extends Plugin {
await this.initializeConfig();

if (this.config.engine.autoUpdate) {
await this.deviceManagerEngine.updateEngines();
try {
await this.deviceManagerEngine.updateEngines();
} catch (error) {
this.context.log.error(
`An error occured while updating the engines during startup: ${error}`,
);
}
}
});
}
Expand Down

0 comments on commit e09c5a3

Please sign in to comment.