Skip to content

Commit

Permalink
Added the Error Message generation into the export utils. (#476)
Browse files Browse the repository at this point in the history
* Added the Error Message generation into the export utils.

* Added error messages

* - Implemented error messages for valves
- fixed some of deepsource warnings

* Added cc to deep source ignore

* Updated the save json button to call the view manager download json method

* Addressed deep source issues and updated the json output data
  • Loading branch information
rkrishnasanka authored Apr 5, 2023
1 parent e2443b8 commit 11da5c0
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .deepsource.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ test_patterns = [
exclude_patterns = [
"lib/**",
"orbit_bundle.js",
"public/**"
"public/**",
"cc/**"
]

[[analyzers]]
Expand Down
88 changes: 63 additions & 25 deletions src/app/core/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as IOUtils from "../utils/ioUtils";
import DeviceUtils from "@/app/utils/deviceUtils";
import { ComponentAPI } from "@/componentAPI";
import MapUtils from "../utils/mapUtils";
import { SerializationError } from "../utils/exportUtils";

/**
* The Device stores information about a design.
Expand Down Expand Up @@ -469,10 +470,20 @@ export default class Device {
* @returns {Array<ComponentInterchangeV1>}
* @memberof Device
*/
__componentsToInterchangeV1(): Array<ComponentInterchangeV1> {
__componentsToInterchangeV1(errorList: Array<SerializationError>): Array<ComponentInterchangeV1> {
let output: Array<ComponentInterchangeV1> = [];
for (let i in this.__components) {
output.push(this.__components[i].toInterchangeV1());
try {
output.push(this.__components[i].toInterchangeV1());
}
catch (e) {
console.error(e);
errorList.push(new SerializationError(
e.message,
`Component ${this.__components[i].id} could not be converted to InterchangeV1`,
JSON.stringify(this.__components[i]),
));
}
}
return output;
}
Expand All @@ -481,28 +492,46 @@ export default class Device {
* @returns {Array} Returns an array with the connections
* @memberof Device
*/
__connectionToInterchangeV1(): Array<ConnectionInterchangeV1_2> {
__connectionToInterchangeV1(errorList: Array<SerializationError>): Array<ConnectionInterchangeV1_2> {
let output: Array<ConnectionInterchangeV1_2> = [];
for (let i in this.__connections) {
output.push(this.__connections[i].toInterchangeV1());
try {
output.push(this.__connections[i].toInterchangeV1());
} catch (e) {
console.error(e);
errorList.push(new SerializationError(
e.message,
`Connection : ${this.__connections[i].id} could not be converted to InterchangeV1`,
JSON.stringify(this.__connections[i]),
));
}
}
return output;
}

__valvesToInterchangeV1(): Array<ValveInterchangeV1_2> {
__valvesToInterchangeV1(errorList: Array<SerializationError>): Array<ValveInterchangeV1_2> {
let output: Array<ValveInterchangeV1_2> = [];
this.__valveMap.forEach((target, valve) => {
let valve_type = this.__valveTypeMap.get(valve);
this.__valveMap.forEach((target, valveID) => {
let valve_type = this.__valveTypeMap.get(valveID);
if(valve_type === undefined) {
console.error("Valve type not found for valve: " + valve + " , setting default to NORMALLY_OPEN");
console.warn("Valve type not found for valve: " + valveID + " , setting default to NORMALLY_OPEN");
valve_type = ValveType.NORMALLY_OPEN;
}
output.push({
componentid: valve,
connectionid: target,
type: valve_type,
params: {}
});
try {
output.push({
componentid: valveID,
connectionid: target,
type: valve_type,
params: {}
});
} catch (e) {
console.error(e);
errorList.push(new SerializationError(
e.message,
`Valve : ${valveID} could not be converted to InterchangeV1`,
JSON.stringify(valveID),
));
}
});
return output;
}
Expand All @@ -524,10 +553,19 @@ export default class Device {
* @return {Array<LayerInterchangeV1>} Returns an array with the layers
* @memberof Device
*/
__layersToInterchangeV1(): Array<LayerInterchangeV1> {
__layersToInterchangeV1(errorList: Array<SerializationError>): Array<LayerInterchangeV1> {
const output: Array<LayerInterchangeV1> = [];
for (const i in this.__layers) {
output.push(this.__layers[i].toInterchangeV1());
try {
output.push(this.__layers[i].toInterchangeV1());
} catch (e) {
console.error(e);
errorList.push(new SerializationError(
e.message,
`Layer ${this.__layers[i].id} could not be converted to InterchangeV1`,
JSON.stringify(this.__layers[i])
));
}
}
return output;
}
Expand Down Expand Up @@ -579,35 +617,35 @@ export default class Device {
* @returns {Device} Returns an Device object in Interchange V1 format
* @memberof Device
*/
toInterchangeV1(): DeviceInterchangeV1 {
toInterchangeV1(errorList: Array<SerializationError>): DeviceInterchangeV1 {
let output: DeviceInterchangeV1 = {
name: this.__name,
params: {
width: this.getXSpan(),
length: this.getYSpan()
},
//TODO: Use this to dynamically create enough layers to scroll through
layers: this.__layersToInterchangeV1(),
components: this.__componentsToInterchangeV1(),
connections: this.__connectionToInterchangeV1(),
valves: this.__valvesToInterchangeV1(),
layers: this.__layersToInterchangeV1(errorList),
components: this.__componentsToInterchangeV1(errorList),
connections: this.__connectionToInterchangeV1(errorList),
valves: this.__valvesToInterchangeV1(errorList),
version: "1",
groups: this.__groupsToJSON()
};
return output;
}

toInterchangeV1_1(): DeviceInterchangeV1_1 {
toInterchangeV1_1(errorList: Array<SerializationError>): DeviceInterchangeV1_1 {
let output: DeviceInterchangeV1_1 = {
name: this.__name,
params: {
width: this.getXSpan(),
length: this.getYSpan()
},
//TODO: Use this to dynamically create enough layers to scroll through
layers: this.__layersToInterchangeV1(),
components: this.__componentsToInterchangeV1(),
connections: this.__connectionToInterchangeV1(),
layers: this.__layersToInterchangeV1(errorList),
components: this.__componentsToInterchangeV1(errorList),
connections: this.__connectionToInterchangeV1(errorList),
version: "1.1",
groups: this.__groupsToJSON()
};
Expand Down
4 changes: 2 additions & 2 deletions src/app/manufacturing/cncGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { DFMType } from "./manufacturingInfo";
export default class CNCGenerator {
__device: Device;
__viewManagerDelegate: viewManager;
__svgData: Map<String, string>;
__svgData: Map<string, string>;

/**
* Default Constructor of GNCGenerator object.
Expand All @@ -34,7 +34,7 @@ export default class CNCGenerator {
* @returns {}
* @memberof CNCGenerator
*/
getSVGOutputs(): Map<String, string> {
getSVGOutputs(): Map<string, string> {
return this.__svgData;
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/manufacturing/laserCuttingGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class LaserCuttingGenerator {
* @returns Returns the SVG data
* @memberof LaserCuttingGenerator
*/
getSVGOutputs(): Map<String, string> {
getSVGOutputs(): Map<string, string> {
return this.__svgData;
}

Expand Down
6 changes: 3 additions & 3 deletions src/app/manufacturing/manufacturingLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { ToolPaperObject } from "../core/init";
*/
export default class ManufacturingLayer {
__features: Array<ToolPaperObject>;
__name: String;
__name: string;
__paperGroup: paper.Group;
__flip: boolean;

/**
* Default Constructor for the Manufacturing Layer
* @param {String} name Name of the field
*/
constructor(name: String, flip = false) {
constructor(name: string, flip = false) {
this.__features = [];
this.__name = name;
this.__paperGroup = new paper.Group();
Expand All @@ -31,7 +31,7 @@ export default class ManufacturingLayer {
* @return {String} Returns the name of the field
* @memberof ManufacturingLayer
*/
get name(): String {
get name(): string {
return this.__name;
}

Expand Down
62 changes: 60 additions & 2 deletions src/app/utils/exportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,64 @@ import {
import ConnectionTarget from "../core/connectionTarget";
import GeometryElement from "../core/geometryElement";

export class SerializationError {
/**
* Error message for the user.
*
* @type {string}
* @memberof SerializationError
*/
public message: string;

/**
* The element that caused the error.
* TBD on how ot use this in the future.
* @type {*}
* @memberof SerializationError
*/
public element: string;

/**
* The JSON data that was being processed when the error occurred.
*
* @type {string}
* @memberof SerializationError
*/
public jsonData: string;

/**
* Creates an instance of SerializationError.
* @param {string} message
* @param {string} element
* @param {string} jsonData
* @memberof SerializationError
*/
constructor(message: string, element: string, jsonData: string) {
this.message = message;
this.element = element;
this.jsonData = jsonData;
}

/**
* Converts the error to a string.
* suitable for display to the user. or in a log file.
*
* @returns {string}
* @memberof SerializationError
*/
toText(): string {
let ret = `Error: ${this.message}\n`;
ret += `Element: ${this.element}\n`;
ret += "JSON Data:\n";
ret += "\`\`\`\n";
ret += this.jsonData;
ret += "\n\`\`\`\n";

return ret;
}
}


export default class ExportUtils {

/**
Expand All @@ -26,7 +84,7 @@ export default class ExportUtils {
* @returns {InterchangeV1_2}
* @memberof ExportUtils
*/
static toInterchangeV1_2(viewManagerDelegate: ViewManager): InterchangeV1_2 {
static toInterchangeV1_2(viewManagerDelegate: ViewManager, errorList:SerializationError[]): InterchangeV1_2 {
if(viewManagerDelegate.currentDevice === null) {
throw new Error("No device selected");
}
Expand All @@ -35,7 +93,7 @@ export default class ExportUtils {
for (let i = 0; i < viewManagerDelegate.renderLayers.length; i++) {
renderLayers.push(viewManagerDelegate.renderLayers[i].toInterchangeV1());
}
const device = viewManagerDelegate.currentDevice.toInterchangeV1();
const device = viewManagerDelegate.currentDevice.toInterchangeV1(errorList);

const valvemap = {};
const valvetypemap = {};
Expand Down
2 changes: 1 addition & 1 deletion src/app/view/paperView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ export default class PaperView {
* @returns {void}
* @memberof PaperView
*/
setResizeFunction(func: Function | null): void {
setResizeFunction(func: () => void | null): void {
if (this.canvas === null) {
throw new Error("Canvas is null");
}
Expand Down
26 changes: 21 additions & 5 deletions src/app/view/viewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { ComponentAPI } from "@/componentAPI";
import RenderLayer from "@/app/view/renderLayer";

import LoadUtils from "@/app/utils/loadUtils";
import ExportUtils from "@/app/utils/exportUtils";
import ExportUtils, { SerializationError } from "@/app/utils/exportUtils";
import { LogicalLayerType, InterchangeV1_2, ValveType } from "@/app/core/init";

import { Point } from "@/app/core/init";
Expand Down Expand Up @@ -1374,7 +1374,10 @@ export default class ViewManager {
saveDeviceState(): void {
console.log("Saving to stack");

const save = JSON.stringify(Registry.currentDevice?.toInterchangeV1());
// TODO - Future versions of the software should be using the saving
// the device as a set of atomic operations and not as the json data
// unless we are using it as a refernce.
const save = JSON.stringify(Registry.currentDevice?.toInterchangeV1([]));

this.undoStack.pushDesign(save);
}
Expand Down Expand Up @@ -1546,8 +1549,8 @@ export default class ViewManager {
* @returns {void}
* @memberof ViewManager
*/
generateExportJSON(): InterchangeV1_2 {
const json = ExportUtils.toInterchangeV1_2(this);
generateExportJSON(errorList:Array<SerializationError> = []): InterchangeV1_2 {
const json = ExportUtils.toInterchangeV1_2(this, errorList);
// const json = this.currentDevice.toInterchangeV1_1();
// json.customComponents = this.customComponentManager.toJSON();
return json;
Expand Down Expand Up @@ -1714,10 +1717,23 @@ export default class ViewManager {
if(this.currentDevice === null){
throw new Error("No device loaded");
}
let json = new Blob([JSON.stringify(this.generateExportJSON())], {
const errorList: Array<SerializationError> = [];
const json = new Blob([JSON.stringify(this.generateExportJSON(errorList))], {
type: "application/json"
});
saveAs(json, this.currentDevice.name + ".json");

if (errorList.length > 0) {
// Concatenate all the errors into a single string with newlines and save it as a file
let errorString = "";
for (const error of errorList) {
errorString += error.toText() + "\n\n";
}
const errorBlob = new Blob([errorString], {
type: "text/plain"
});
saveAs(errorBlob, this.currentDevice.name + "_errors.txt");
}
}

createNewDevice(name: string): void {
Expand Down
5 changes: 1 addition & 4 deletions src/components/ManufacturingPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ export default {
},
methods: {
downloadJSON() {
let json = new Blob([JSON.stringify(this.viewManagerRef.generateExportJSON())], {
type: "application/json"
});
saveAs(json, Registry.currentDevice.name + ".json");
this.viewManagerRef.downloadJSON();
},
downloadSVG() {
let svgs = this.viewManagerRef.layersToSVGStrings();
Expand Down

0 comments on commit 11da5c0

Please sign in to comment.