From dcf13450227a4635fb827f757bb08cc4eab7398f Mon Sep 17 00:00:00 2001
From: pkupczyk <piotr.kupczyk@id.ethz.ch>
Date: Fri, 23 Dec 2022 13:27:08 +0100
Subject: [PATCH] SSDM-13282 : Admin UI - improve error handling

---
 .../common/form/EntityAutocompleterField.jsx  |  2 +
 .../components/database/DatabaseComponent.jsx | 96 ++++++++++---------
 .../js/components/database/DatabaseTab.jsx    | 94 +++++++++---------
 3 files changed, 101 insertions(+), 91 deletions(-)

diff --git a/openbis_ng_ui/src/js/components/common/form/EntityAutocompleterField.jsx b/openbis_ng_ui/src/js/components/common/form/EntityAutocompleterField.jsx
index 7b80e890020..cb1e7e55b53 100644
--- a/openbis_ng_ui/src/js/components/common/form/EntityAutocompleterField.jsx
+++ b/openbis_ng_ui/src/js/components/common/form/EntityAutocompleterField.jsx
@@ -3,6 +3,7 @@ import React from 'react'
 import autoBind from 'auto-bind'
 import { withStyles } from '@material-ui/core/styles'
 import AutocompleterField from '@src/js/components/common/form/AutocompleterField.jsx'
+import AppController from '@src/js/components/AppController.js'
 import openbis from '@src/js/services/openbis.js'
 import messages from '@src/js/common/messages.js'
 import logger from '@src/js/common/logger.js'
@@ -73,6 +74,7 @@ class EntityAutocompleterField extends React.PureComponent {
           )
         }
       } catch (error) {
+        AppController.getInstance().errorChange(error)
         this.setState({
           loading: false
         })
diff --git a/openbis_ng_ui/src/js/components/database/DatabaseComponent.jsx b/openbis_ng_ui/src/js/components/database/DatabaseComponent.jsx
index 63ee6a61e6a..ed136e9308f 100644
--- a/openbis_ng_ui/src/js/components/database/DatabaseComponent.jsx
+++ b/openbis_ng_ui/src/js/components/database/DatabaseComponent.jsx
@@ -1,5 +1,6 @@
 import React from 'react'
 import Container from '@src/js/components/common/form/Container.jsx'
+import AppController from '@src/js/components/AppController.js'
 import openbis from '@src/js/services/openbis.js'
 import objectType from '@src/js/common/consts/objectType.js'
 import logger from '@src/js/common/logger.js'
@@ -13,54 +14,57 @@ class DatabaseComponent extends React.PureComponent {
   }
 
   async componentDidMount() {
-    const { object } = this.props
+    try {
+      const { object } = this.props
 
-    let json = null
+      let json = null
+      if (object.type === objectType.SPACE) {
+        const spaces = await openbis.getSpaces(
+          [new openbis.SpacePermId(object.id)],
+          new openbis.SpaceFetchOptions()
+        )
+        json = spaces[object.id]
+      } else if (object.type === objectType.PROJECT) {
+        const projects = await openbis.getProjects(
+          [new openbis.ProjectPermId(object.id)],
+          new openbis.ProjectFetchOptions()
+        )
+        json = projects[object.id]
+      } else if (object.type === objectType.COLLECTION) {
+        const experiments = await openbis.getExperiments(
+          [new openbis.ExperimentPermId(object.id)],
+          new openbis.ExperimentFetchOptions()
+        )
+        json = experiments[object.id]
+      } else if (object.type === objectType.OBJECT) {
+        const fetchOptions = new openbis.SampleFetchOptions()
+        fetchOptions.withSpace()
+        fetchOptions.withProject()
+        fetchOptions.withExperiment()
+        fetchOptions.withParents()
+        const samples = await openbis.getSamples(
+          [new openbis.SamplePermId(object.id)],
+          fetchOptions
+        )
+        json = samples[object.id]
+      } else if (object.type === objectType.DATA_SET) {
+        const fetchOptions = new openbis.DataSetFetchOptions()
+        fetchOptions.withExperiment()
+        fetchOptions.withSample()
+        fetchOptions.withParents()
+        const dataSets = await openbis.getDataSets(
+          [new openbis.DataSetPermId(object.id)],
+          fetchOptions
+        )
+        json = dataSets[object.id]
+      }
 
-    if (object.type === objectType.SPACE) {
-      const spaces = await openbis.getSpaces(
-        [new openbis.SpacePermId(object.id)],
-        new openbis.SpaceFetchOptions()
-      )
-      json = spaces[object.id]
-    } else if (object.type === objectType.PROJECT) {
-      const projects = await openbis.getProjects(
-        [new openbis.ProjectPermId(object.id)],
-        new openbis.ProjectFetchOptions()
-      )
-      json = projects[object.id]
-    } else if (object.type === objectType.COLLECTION) {
-      const experiments = await openbis.getExperiments(
-        [new openbis.ExperimentPermId(object.id)],
-        new openbis.ExperimentFetchOptions()
-      )
-      json = experiments[object.id]
-    } else if (object.type === objectType.OBJECT) {
-      const fetchOptions = new openbis.SampleFetchOptions()
-      fetchOptions.withSpace()
-      fetchOptions.withProject()
-      fetchOptions.withExperiment()
-      fetchOptions.withParents()
-      const samples = await openbis.getSamples(
-        [new openbis.SamplePermId(object.id)],
-        fetchOptions
-      )
-      json = samples[object.id]
-    } else if (object.type === objectType.DATA_SET) {
-      const fetchOptions = new openbis.DataSetFetchOptions()
-      fetchOptions.withExperiment()
-      fetchOptions.withSample()
-      fetchOptions.withParents()
-      const dataSets = await openbis.getDataSets(
-        [new openbis.DataSetPermId(object.id)],
-        fetchOptions
-      )
-      json = dataSets[object.id]
+      this.setState({
+        json
+      })
+    } catch (error) {
+      AppController.getInstance().errorChange(error)
     }
-
-    this.setState({
-      json
-    })
   }
 
   render() {
@@ -68,7 +72,7 @@ class DatabaseComponent extends React.PureComponent {
 
     return (
       <Container>
-        <pre>{JSON.stringify(this.state.json, null, 2)}</pre>
+        <pre>{JSON.stringify(this.state.json || {}, null, 2)}</pre>
       </Container>
     )
   }
diff --git a/openbis_ng_ui/src/js/components/database/DatabaseTab.jsx b/openbis_ng_ui/src/js/components/database/DatabaseTab.jsx
index d9a6f2140fd..cf9355a258f 100644
--- a/openbis_ng_ui/src/js/components/database/DatabaseTab.jsx
+++ b/openbis_ng_ui/src/js/components/database/DatabaseTab.jsx
@@ -14,58 +14,62 @@ class DatabaseTab extends React.PureComponent {
   }
 
   async componentDidMount() {
-    const { tab } = this.props
-    const { object } = tab
+    try {
+      const { tab } = this.props
+      const { object } = tab
 
-    let typeText = null
-    let idText = null
+      let typeText = null
+      let idText = null
 
-    if (object.type === objectType.SPACE) {
-      typeText = 'Space'
-      idText = object.id
-    } else if (object.type === objectType.PROJECT) {
-      typeText = 'Project'
-      const projects = await openbis.getProjects(
-        [new openbis.ProjectPermId(object.id)],
-        new openbis.ProjectFetchOptions()
-      )
+      if (object.type === objectType.SPACE) {
+        typeText = 'Space'
+        idText = object.id
+      } else if (object.type === objectType.PROJECT) {
+        typeText = 'Project'
+        const projects = await openbis.getProjects(
+          [new openbis.ProjectPermId(object.id)],
+          new openbis.ProjectFetchOptions()
+        )
 
-      if (projects[object.id]) {
-        idText = projects[object.id].getCode()
-      }
-    } else if (object.type === objectType.COLLECTION) {
-      typeText = 'Collection'
-      const experiments = await openbis.getExperiments(
-        [new openbis.ExperimentPermId(object.id)],
-        new openbis.ExperimentFetchOptions()
-      )
-      if (experiments[object.id]) {
-        idText = experiments[object.id].getCode()
+        if (projects[object.id]) {
+          idText = projects[object.id].getCode()
+        }
+      } else if (object.type === objectType.COLLECTION) {
+        typeText = 'Collection'
+        const experiments = await openbis.getExperiments(
+          [new openbis.ExperimentPermId(object.id)],
+          new openbis.ExperimentFetchOptions()
+        )
+        if (experiments[object.id]) {
+          idText = experiments[object.id].getCode()
+        }
+      } else if (object.type === objectType.OBJECT) {
+        typeText = 'Object'
+        const samples = await openbis.getSamples(
+          [new openbis.SamplePermId(object.id)],
+          new openbis.SampleFetchOptions()
+        )
+        if (samples[object.id]) {
+          idText = samples[object.id].getCode()
+        }
+      } else if (object.type === objectType.DATA_SET) {
+        typeText = 'Data Set'
+        idText = object.id
       }
-    } else if (object.type === objectType.OBJECT) {
-      typeText = 'Object'
-      const samples = await openbis.getSamples(
-        [new openbis.SamplePermId(object.id)],
-        new openbis.SampleFetchOptions()
-      )
-      if (samples[object.id]) {
-        idText = samples[object.id].getCode()
+
+      const tabWithLabel = {
+        ...tab,
+        label: (typeText || object.type) + ': ' + (idText || object.id)
       }
-    } else if (object.type === objectType.DATA_SET) {
-      typeText = 'Data Set'
-      idText = object.id
-    }
 
-    const tabWithLabel = {
-      ...tab,
-      label: (typeText || object.type) + ': ' + (idText || object.id)
+      AppController.getInstance().replaceOpenTab(
+        pages.DATABASE,
+        tabWithLabel.id,
+        tabWithLabel
+      )
+    } catch (error) {
+      AppController.getInstance().errorChange(error)
     }
-
-    AppController.getInstance().replaceOpenTab(
-      pages.DATABASE,
-      tabWithLabel.id,
-      tabWithLabel
-    )
   }
 
   render() {
-- 
GitLab