diff --git a/Database/QuestieDB.lua b/Database/QuestieDB.lua index 0e6fa78..36d6a0e 100644 --- a/Database/QuestieDB.lua +++ b/Database/QuestieDB.lua @@ -321,6 +321,19 @@ if not Item then end if not Item.CreateFromItemID then + local function SafeGetItemName(itemID) + local numericItemID = tonumber(itemID) + if not numericItemID or numericItemID == 0 then + return "item:" .. tostring(itemID) + end + + local ok, name = pcall(GetItemInfo, numericItemID) + if not ok then + return "item:" .. tostring(numericItemID) + end + return name or ("item:" .. tostring(numericItemID)) + end + function Item:CreateFromItemID(itemID) local obj = {} @@ -332,8 +345,7 @@ if not Item.CreateFromItemID then end function obj:GetItemName() - local name = GetItemInfo(itemID) - return name or ("item:" .. tostring(itemID)) + return SafeGetItemName(itemID) end return obj diff --git a/Tests/QuestieItemNameSafety_spec.lua b/Tests/QuestieItemNameSafety_spec.lua new file mode 100644 index 0000000..63b1fdd --- /dev/null +++ b/Tests/QuestieItemNameSafety_spec.lua @@ -0,0 +1,19 @@ +describe("Questie item name safety", function() + before_each(function() + dofile("Tests/wow_api_mock.lua") + GetItemInfo = function() + error("GetItemInfo should not be called for invalid item ids") + end + dofile("Database/QuestieDB.lua") + end) + + it("returns a placeholder instead of calling GetItemInfo for invalid item ids", function() + local item = Item:CreateFromItemID(nil) + assert.equals("item:nil", item:GetItemName()) + end) + + it("also handles non-numeric item ids safely", function() + local item = Item:CreateFromItemID("bad-id") + assert.equals("item:bad-id", item:GetItemName()) + end) +end)