Skip to content

Commit

Permalink
Handle json parsing error and add on error callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-wu committed Nov 29, 2024
1 parent 5cecb67 commit 861f881
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 16 deletions.
14 changes: 7 additions & 7 deletions 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 @@ -54,7 +54,7 @@
"vue": "^3.4.21",
"vue-router": "^4.2.5",
"vue3-component-svg-sprite": "^0.0.1",
"zincjs": "^1.11.4"
"zincjs": "^1.11.5"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.6.2",
Expand Down
12 changes: 12 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
:marker-labels="markerLabels"
:enableLocalAnnotations="false"
@open-map="openMap"
@on-error="onError"
@on-ready="onReady"
@scaffold-selected="onSelected"
@scaffold-navigated="onNavigated"
Expand Down Expand Up @@ -640,6 +641,17 @@ export default {
}
});
},
onError: function(payload) {
if (payload?.type === "download-error") {
const dropZone = this.$refs.dropzone;
if (dropZone) {
const realFilename = dropZone.findRealFilename(payload.xhr.responseURL);
if (realFilename) {
console.error(`External Resource ${realFilename}`);
}
}
}
},
onReady: function () {
if (this.consoleOn) console.log(this.$refs.scaffold)
if (this.readyCallback) {
Expand Down
12 changes: 10 additions & 2 deletions src/app/DropZone.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<script>
/* eslint-disable no-alert, no-console */
import { markRaw } from 'vue';
import { SimpleDropzone } from "simple-dropzone";
import path from "path";
Expand All @@ -26,7 +27,8 @@ export default {
name: "DropZone",
data: function () {
return {
objectURLs: [],
objectURLs: markRaw([]),
filesMapping: markRaw({}),
};
},
mounted: function () {
Expand All @@ -39,6 +41,9 @@ export default {
});
},
methods: {
findRealFilename: function(objectURL) {
return this.filesMapping[objectURL]
},
processTextureFile: function(textureData, flatarray) {
if (textureData && textureData.images && textureData.images.source) {
const images = textureData.images.source;
Expand All @@ -50,6 +55,7 @@ export default {
const objectURL = URL.createObjectURL(flatarray[index][1]);
this.objectURLs.push(objectURL);
textureData.images.source[i] = objectURL;
this.filesMapping[objectURL] = images[i];
}
}
const content = JSON.stringify(textureData);
Expand All @@ -65,6 +71,7 @@ export default {
const re = new RegExp(key, "g");
content = content.replace(re, objectURL);
this.objectURLs.push(objectURL);
this.filesMapping[objectURL] = key;
}
}
const data = JSON.parse(content);
Expand Down Expand Up @@ -93,7 +100,8 @@ export default {
},
revokeURLs: function () {
this.objectURLs.forEach(objectURL => URL.revokeObjectURL(objectURL));
this.objectURLs = [];
this.objectURLs.length = 0;
this.filesMapping = markRaw({});
},
localDrop: function (fileMap) {
this.revokeURLs();
Expand Down
8 changes: 8 additions & 0 deletions src/components/ScaffoldVuer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2209,6 +2209,11 @@ export default {
}
}
},
downloadErrorCallback: function() {
return (error) => {
this.$emit('on-error', error);
}
},
setURLFinishCallback: function (options) {
return () => {
this.localAnnotationsList.length = 0;
Expand Down Expand Up @@ -2368,6 +2373,9 @@ export default {
this.isReady = false;
this.$_searchIndex.removeAll();
this.hideRegionTooltip();
this.$module.setDownloadErrorCallback(
this.downloadErrorCallback()
);
this.$module.setFinishDownloadCallback(
this.setURLFinishCallback({
background: state?.background,
Expand Down
34 changes: 28 additions & 6 deletions src/scripts/OrgansRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const OrgansSceneData = function() {
const organPartAddedCallbacks = new Array();
const organPartRemovedCallbacks = new Array();
let finishDownloadCallback = undefined;
let downloadErrorCallback = undefined;
const modelsLoader = ModelsLoaderIn;
this.NDCCameraControl = undefined;
_this.typeName = "Organ Viewer";
Expand Down Expand Up @@ -187,6 +188,16 @@ const OrgansSceneData = function() {
finishDownloadCallback = undefined;
}


this.setDownloadErrorCallback = function(callback) {
if (typeof(callback === "function"))
downloadErrorCallback = callback;
}

this.unsetDownloadErrorCallback = function() {
downloadErrorCallback = undefined;
}

this.getNamedObjectsToScreenCoordinates = function(name, camera) {
const vector = new THREE.Vector3();
vector.setFromMatrixPosition( obj.matrixWorld );
Expand Down Expand Up @@ -435,11 +446,22 @@ const OrgansSceneData = function() {
finishDownloadCallback();
}
}

const singleItemDownloadCompletedCallback = function(systemName, partName, useDefautColour) {
return function(geometry) {
addOrganPart(systemName, partName, useDefautColour, geometry);
_this.settingsChanged();

//The payload can either be a zinc object when the loading is successful or
//an object containg the details of error message on failure.
//We only use it to handle an error
const singleItemFinishCallback = function() {
return function(payload) {

if (payload?.type === "Error") {
if (downloadErrorCallback) {
const error = {
xhr: payload.xhr,
type: "download-error",
};
downloadErrorCallback(error);
}
}
}
}

Expand Down Expand Up @@ -549,7 +571,7 @@ const OrgansSceneData = function() {
_this.sceneData.metaURL = url;
organScene.addZincObjectAddedCallbacks(_addOrganPartCallback(systemName, partName, false));
organScene.addZincObjectRemovedCallbacks(_removeOrganPartCallback(undefined, partName, false));
organScene.loadMetadataURL(url, undefined, downloadCompletedCallback());
organScene.loadMetadataURL(url, singleItemFinishCallback(), downloadCompletedCallback());
_this.scene = organScene;
_this.zincRenderer.setCurrentScene(organScene);
_this.graphicsHighlight.reset();
Expand Down

0 comments on commit 861f881

Please sign in to comment.