From a76f2cf879893953dec9cd3def88b05c94a35a45 Mon Sep 17 00:00:00 2001
From: rhenck <richard.henck@iqb.hu-berlin.de>
Date: Mon, 7 Feb 2022 17:19:43 +0100
Subject: [PATCH] [editor] Add error handling of empty or invalid unit
 definitions

Now shows an error message and continues with an empty unit.
---
 .../editor/src/app/services/unit.service.ts     | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/projects/editor/src/app/services/unit.service.ts b/projects/editor/src/app/services/unit.service.ts
index 972a5a797..7dbd936f8 100644
--- a/projects/editor/src/app/services/unit.service.ts
+++ b/projects/editor/src/app/services/unit.service.ts
@@ -48,9 +48,20 @@ export class UnitService {
   }
 
   loadUnitDefinition(unitDefinition: string): void {
-    if (unitDefinition) {
-      this.idService.reset();
-      this.unit = new Unit(JSON.parse(unitDefinition));
+    this.idService.reset();
+    let error = false;
+    if (!unitDefinition) {
+      error = true;
+    } else {
+      try {
+        this.unit = new Unit(JSON.parse(unitDefinition));
+      } catch (e) {
+        error = true;
+      }
+    }
+    if (error) {
+      this.messageService.showError('Unit konnte nicht geladen werden!');
+      this.unit = new Unit({});
     }
   }
 
-- 
GitLab