From aebacfb1fe04cf4554844f0d6ca771ff1b11c695 Mon Sep 17 00:00:00 2001 From: Robyn Thiessen-Bock Date: Wed, 25 Oct 2023 16:11:26 -0400 Subject: [PATCH] Improve error handling of view service response Fixes #2144 --- src/js/views/MetadataIndexView.js | 147 ++++++++++++++++-------------- src/js/views/MetadataView.js | 62 +++++++------ 2 files changed, 111 insertions(+), 98 deletions(-) diff --git a/src/js/views/MetadataIndexView.js b/src/js/views/MetadataIndexView.js index 3d1bfc197..99910ff88 100644 --- a/src/js/views/MetadataIndexView.js +++ b/src/js/views/MetadataIndexView.js @@ -65,85 +65,92 @@ define(['jquery', encodeURIComponent(this.pid)+'")&rows=1&start=0&fl=*&wt=json'; var requestSettings = { url: MetacatUI.appModel.get('queryServiceUrl') + query, - success: function(data, textStatus, xhr){ + success: function (data, textStatus, xhr) { - if(data.response.numFound == 0){ + try { - if( view.parentView && view.parentView.model ){ + if (!data?.response?.numFound) { - //Show a "not indexed" message if there is system metadata but nothing in - // the index - if(view.parentView.model.get("systemMetadata")){ - view.showNotIndexed(); - } - //Show a "not found" message if there is no system metadata and no results in the index - else{ - view.parentView.model.set("notFound", true); - view.parentView.showNotFound(); - } - } + if (view.parentView && view.parentView.model) { - view.flagComplete(); - } - else{ - view.docs = data.response.docs; + //Show a "not indexed" message if there is system metadata but nothing in + // the index + if (view.parentView.model.get("systemMetadata")) { + view.showNotIndexed(); + } + //Show a "not found" message if there is no system metadata and no results in the index + else { + view.parentView.model.set("notFound", true); + view.parentView.showNotFound(); + } + } - _.each(data.response.docs, function(doc, i, list){ + view.flagComplete(); + } + else { + view.docs = data.response.docs; - //If this is a data object and there is a science metadata doc that describes it, then navigate to that Metadata View. - if((doc.formatType == "DATA") && (doc.isDocumentedBy && doc.isDocumentedBy.length)){ - view.onClose(); - MetacatUI.uiRouter.navigate("view/" + doc.isDocumentedBy[0], true); - return; - } + _.each(data.response.docs, function (doc, i, list) { - var metadataEl = $(document.createElement("section")).attr("id", "metadata-index-details"), - id = doc.id, - creator = doc.origin, - title = doc.title, - pubDate = doc.pubDate, - dateUploaded = doc.dateUploaded, - keys = Object.keys(doc), - docModel = new SolrResult(doc); - - //Extract General Info details that we want to list first - var generalInfoKeys = ["title", "id", "abstract", "pubDate", "keywords"]; - keys = _.difference(keys, generalInfoKeys); - $(metadataEl).append(view.formatAttributeSection(docModel, generalInfoKeys, "General")); - - //Extract Spatial details - var spatialKeys = ["site", "southBoundCoord", "northBoundCoord", "westBoundCoord", "eastBoundCoord"]; - keys = _.difference(keys, spatialKeys); - $(metadataEl).append(view.formatAttributeSection(docModel, spatialKeys, "Geographic Region")); - - //Extract Temporal Coverage details - var temporalKeys = ["beginDate", "endDate"]; - keys = _.difference(keys, temporalKeys); - $(metadataEl).append(view.formatAttributeSection(docModel, temporalKeys, "Temporal Coverage")); - - //Extract Taxonomic Coverage details - var taxonKeys = ["order", "phylum", "family", "genus", "species", "scientificName"]; - keys = _.difference(keys, taxonKeys); - $(metadataEl).append(view.formatAttributeSection(docModel, taxonKeys, "Taxonomic Coverage")); - - //Extract People details - var peopleKeys = ["origin", "investigator", "contactOrganization", "project"]; - keys = _.difference(keys, peopleKeys); - $(metadataEl).append(view.formatAttributeSection(docModel, peopleKeys, "People and Associated Parties")); - - //Extract Access Control details - var accessKeys = ["isPublic", "submitter", "rightsHolder", "writePermission", "readPermission", "changePermission", "authoritativeMN"]; - keys = _.difference(keys, accessKeys); - $(metadataEl).append(view.formatAttributeSection(docModel, accessKeys, "Access Control")); - - //Add the rest of the metadata - $(metadataEl).append(view.formatAttributeSection(docModel, keys, "Other")); - - view.$el.html(metadataEl); + //If this is a data object and there is a science metadata doc that describes it, then navigate to that Metadata View. + if ((doc.formatType == "DATA") && (doc.isDocumentedBy && doc.isDocumentedBy.length)) { + view.onClose(); + MetacatUI.uiRouter.navigate("view/" + doc.isDocumentedBy[0], true); + return; + } - view.flagComplete(); - }); + var metadataEl = $(document.createElement("section")).attr("id", "metadata-index-details"), + id = doc.id, + creator = doc.origin, + title = doc.title, + pubDate = doc.pubDate, + dateUploaded = doc.dateUploaded, + keys = Object.keys(doc), + docModel = new SolrResult(doc); + + //Extract General Info details that we want to list first + var generalInfoKeys = ["title", "id", "abstract", "pubDate", "keywords"]; + keys = _.difference(keys, generalInfoKeys); + $(metadataEl).append(view.formatAttributeSection(docModel, generalInfoKeys, "General")); + + //Extract Spatial details + var spatialKeys = ["site", "southBoundCoord", "northBoundCoord", "westBoundCoord", "eastBoundCoord"]; + keys = _.difference(keys, spatialKeys); + $(metadataEl).append(view.formatAttributeSection(docModel, spatialKeys, "Geographic Region")); + + //Extract Temporal Coverage details + var temporalKeys = ["beginDate", "endDate"]; + keys = _.difference(keys, temporalKeys); + $(metadataEl).append(view.formatAttributeSection(docModel, temporalKeys, "Temporal Coverage")); + + //Extract Taxonomic Coverage details + var taxonKeys = ["order", "phylum", "family", "genus", "species", "scientificName"]; + keys = _.difference(keys, taxonKeys); + $(metadataEl).append(view.formatAttributeSection(docModel, taxonKeys, "Taxonomic Coverage")); + + //Extract People details + var peopleKeys = ["origin", "investigator", "contactOrganization", "project"]; + keys = _.difference(keys, peopleKeys); + $(metadataEl).append(view.formatAttributeSection(docModel, peopleKeys, "People and Associated Parties")); + + //Extract Access Control details + var accessKeys = ["isPublic", "submitter", "rightsHolder", "writePermission", "readPermission", "changePermission", "authoritativeMN"]; + keys = _.difference(keys, accessKeys); + $(metadataEl).append(view.formatAttributeSection(docModel, accessKeys, "Access Control")); + + //Add the rest of the metadata + $(metadataEl).append(view.formatAttributeSection(docModel, keys, "Other")); + + view.$el.html(metadataEl); + + view.flagComplete(); + }); + } + } catch (e) { + console.log("Error parsing Solr response: " + e); + console.log("Solr response: " + data); + view.parentView.showNotFound(); } }, error: function(){ diff --git a/src/js/views/MetadataView.js b/src/js/views/MetadataView.js index 23a878855..2564a90c9 100644 --- a/src/js/views/MetadataView.js +++ b/src/js/views/MetadataView.js @@ -386,47 +386,53 @@ define(['jquery', var loadSettings = { url: endpoint, success: function (response, status, xhr) { + try { - //If the user has navigated away from the MetadataView, then don't render anything further - if (MetacatUI.appView.currentView != viewRef) - return; - - //Our fallback is to show the metadata details from the Solr index - if (status == "error") - viewRef.renderMetadataFromIndex(); - else { - //Check for a response that is a 200 OK status, but is an error msg - if ((response.length < 250) && (response.indexOf("Error transforming document") > -1) && viewRef.model.get("indexed")) { - viewRef.renderMetadataFromIndex(); + //If the user has navigated away from the MetadataView, then don't render anything further + if (MetacatUI.appView.currentView != viewRef) return; - } - //Mark this as a metadata doc with no stylesheet, or one that is at least different than usual EML and FGDC - else if ((response.indexOf('id="Metadata"') == -1)) { - viewRef.$el.addClass("container no-stylesheet"); - if (viewRef.model.get("indexed")) { + //Our fallback is to show the metadata details from the Solr index + if (status == "error" || !response || typeof response !== "string") + viewRef.renderMetadataFromIndex(); + else { + //Check for a response that is a 200 OK status, but is an error msg + if ((response.length < 250) && (response.indexOf("Error transforming document") > -1) && viewRef.model.get("indexed")) { viewRef.renderMetadataFromIndex(); return; } - } + //Mark this as a metadata doc with no stylesheet, or one that is at least different than usual EML and FGDC + else if ((response.indexOf('id="Metadata"') == -1)) { + viewRef.$el.addClass("container no-stylesheet"); + + if (viewRef.model.get("indexed")) { + viewRef.renderMetadataFromIndex(); + return; + } + } - //Now show the response from the view service - viewRef.$(viewRef.metadataContainer).html(response); + //Now show the response from the view service + viewRef.$(viewRef.metadataContainer).html(response); - //If there is no info from the index and there is no metadata doc rendered either, then display a message - if (viewRef.$el.is(".no-stylesheet") && viewRef.model.get("archived") && !viewRef.model.get("indexed")) - viewRef.$(viewRef.metadataContainer).prepend(viewRef.alertTemplate({ msg: "There is limited metadata about this dataset since it has been archived." })); + //If there is no info from the index and there is no metadata doc rendered either, then display a message + if (viewRef.$el.is(".no-stylesheet") && viewRef.model.get("archived") && !viewRef.model.get("indexed")) + viewRef.$(viewRef.metadataContainer).prepend(viewRef.alertTemplate({ msg: "There is limited metadata about this dataset since it has been archived." })); - viewRef.alterMarkup(); + viewRef.alterMarkup(); - viewRef.trigger("metadataLoaded"); + viewRef.trigger("metadataLoaded"); - //Add a map of the spatial coverage - if (gmaps) viewRef.insertSpatialCoverageMap(); + //Add a map of the spatial coverage + if (gmaps) viewRef.insertSpatialCoverageMap(); - // Injects Clipboard objects into DOM elements returned from the View Service - viewRef.insertCopiables(); + // Injects Clipboard objects into DOM elements returned from the View Service + viewRef.insertCopiables(); + } + } catch (e) { + console.log("Error rendering metadata from the view service", e); + console.log("Response from the view service: ", response); + viewRef.renderMetadataFromIndex(); } }, error: function (xhr, textStatus, errorThrown) {