From 2158026437e37efb2b6ffafab1c421921db1912f Mon Sep 17 00:00:00 2001
From: Johannes Loher <johannes.loher@fg4f.de>
Date: Thu, 11 Feb 2021 12:03:32 +0100
Subject: [PATCH] Add additional checks when editing items or effects

---
 src/lang/de.json                       |  3 +++
 src/lang/en.json                       |  3 +++
 src/module/actor/sheets/actor-sheet.ts | 11 +++++++++--
 src/module/item/item-sheet.ts          |  8 ++++++--
 4 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/lang/de.json b/src/lang/de.json
index 391571e..71d5f2a 100644
--- a/src/lang/de.json
+++ b/src/lang/de.json
@@ -200,6 +200,9 @@
     "DS4.RollDialogCancelButton": "Abbrechen",
     "DS4.ErrorUnexpectedHtmlType": "Typfehler: Erwartet wurde '{exType}', tatsächlich erhalten wurde '{realType}'.",
     "DS4.ErrorCouldNotFindForm": "Konnte HTML Element '{htmlElement}' nicht finden.",
+    "DS4.ErrorActorDoesNotHaveItem": "Der Aktor '{actor}' hat kein Item mit der ID '{id}'.",
+    "DS4.ErrorUnexpectedError": "Es gab einen unerwarteten Fehler im Dungeonslayers 4 System. Für mehr Details schauen Sie bitte in die Konsole (F12).",
+    "DS4.ErrorItemDoesNotHaveEffect": "Das Item '{item}' hat keinen Effekt mit der ID '{id}'.",
     "DS4.RollDialogTargetLabel": "Probenwert",
     "DS4.RollDialogModifierLabel": "SL-Modifikator",
     "DS4.RollDialogCoupLabel": "Immersieg bis",
diff --git a/src/lang/en.json b/src/lang/en.json
index c4f5438..4506977 100644
--- a/src/lang/en.json
+++ b/src/lang/en.json
@@ -200,6 +200,9 @@
     "DS4.RollDialogCancelButton": "Cancel",
     "DS4.ErrorUnexpectedHtmlType": "Type Error: Expected '{exType}' but got '{realType}'.",
     "DS4.ErrorCouldNotFindForm": "Could not find HTML element '{htmlElement}'.",
+    "DS4.ErrorActorDoesNotHaveItem": "The actor '{actor}' does not have any item with the id '{id}'.",
+    "DS4.ErrorUnexpectedError": "There was an unexpected error in the Dungeonslayers 4 system. For more details, please take a look at the console (F12).",
+    "DS4.ErrorItemDoesNotHaveEffect": "The item '{item}' does not have any effect with the id '{id}'.",
     "DS4.RollDialogTargetLabel": "Check Target Number",
     "DS4.RollDialogModifierLabel": "Game Master Modifier",
     "DS4.RollDialogCoupLabel": "Coup to",
diff --git a/src/module/actor/sheets/actor-sheet.ts b/src/module/actor/sheets/actor-sheet.ts
index 66c381b..4ab9e44 100644
--- a/src/module/actor/sheets/actor-sheet.ts
+++ b/src/module/actor/sheets/actor-sheet.ts
@@ -72,8 +72,15 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
         // Update Inventory Item
         html.find(".item-edit").on("click", (ev) => {
             const li = $(ev.currentTarget).parents(".item");
-            const item = this.actor.getOwnedItem(li.data("itemId"));
-            item.sheet?.render(true);
+            const id = li.data("itemId");
+            const item = this.actor.getOwnedItem(id);
+            if (!item) {
+                throw new Error(game.i18n.format("DS4.ErrorActorDoesNotHaveItem", { id, actor: this.actor.name }));
+            }
+            if (!item.sheet) {
+                throw new Error(game.i18n.localize("DS4.ErrorUnexpectedError"));
+            }
+            item.sheet.render(true);
         });
 
         // Delete Inventory Item
diff --git a/src/module/item/item-sheet.ts b/src/module/item/item-sheet.ts
index 2c7153a..ced002b 100644
--- a/src/module/item/item-sheet.ts
+++ b/src/module/item/item-sheet.ts
@@ -92,8 +92,12 @@ export class DS4ItemSheet extends ItemSheet<ItemSheet.Data<DS4Item>> {
             case "create":
                 return this._createActiveEffect();
             case "edit":
-                const effect = this.item.effects.get(li.data("effectId"));
-                return effect?.sheet.render(true);
+                const id = li.data("effectId");
+                const effect = this.item.effects.get(id);
+                if (!effect) {
+                    throw new Error(game.i18n.format("DS4.ErrorItemDoesNotHaveEffect", { id, item: this.item.name }));
+                }
+                return effect.sheet.render(true);
             case "delete": {
                 return this.item.deleteEmbeddedEntity("ActiveEffect", li.data("effectId"));
             }