Skip to content

Commit

Permalink
Add check for createOptions (#288)
Browse files Browse the repository at this point in the history
* Add check for createOptions

* Show detailed error messages when createOptions is not valid

* Remove unnecessary promise

* Optimize code

* Update error messages

* Update version to 2.5.0-rc
  • Loading branch information
SLdragon authored Mar 29, 2019
1 parent 0ce19e3 commit c9d2c83
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "azure-iot-toolkit",
"displayName": "Azure IoT Hub Toolkit",
"description": "Interact with Azure IoT Hub, IoT Device Management, IoT Edge Management, IoT Hub Device Simulation, IoT Hub Code Generation",
"version": "2.4.0",
"version": "2.5.0-rc",
"publisher": "vsciot-vscode",
"aiKey": "0caaff90-cc1c-4def-b64c-3ef33615bc9b",
"icon": "logo.png",
Expand Down
34 changes: 33 additions & 1 deletion src/iotEdgeExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,36 @@ export class IoTEdgeExplorer extends BaseExplorer {
return true;
}

private isValidCreateOptions(desiredProperties: any): boolean {
return this.isValidCreateOptionsHepler(desiredProperties.systemModules) && this.isValidCreateOptionsHepler(desiredProperties.modules);
}

private isValidCreateOptionsHepler(modules: any): boolean {
for (const moduleName in modules) {
if (modules.hasOwnProperty(moduleName)) {
try {
const createOptions = modules[moduleName].settings.createOptions;
if (createOptions) {
this.checkJsonString(createOptions);
}
} catch (error) {
vscode.window.showErrorMessage(`CreateOptions of "${moduleName}" is not a valid JSON string: ${error.message}`);
return false;
}
}
}
return true;
}

private checkJsonString(json: string) {
if (json) {
if (json.trim().charAt(0) !== "{") {
throw new Error("not a valid JSON string");
}
JSON.parse(json);
}
}

private async getModuleTwinById(deviceId: string, moduleId: string, moduleType: string = "unknown") {
TelemetryClient.sendEvent(Constants.IoTHubAIGetModuleTwinStartEvent, { moduleType });
const iotHubConnectionString = await Utility.getConnectionString(Constants.IotHubConnectionStringKey, Constants.IotHubConnectionStringTitle);
Expand Down Expand Up @@ -169,7 +199,9 @@ export class IoTEdgeExplorer extends BaseExplorer {
}

try {
const isValid = await this.isValidDeploymentJsonSchema(contentJson);
const isValid = await this.isValidDeploymentJsonSchema(contentJson)
&& this.isValidCreateOptions(contentJson.modulesContent.$edgeAgent["properties.desired"]);

if (!isValid) {
return "";
}
Expand Down

0 comments on commit c9d2c83

Please sign in to comment.