fix: guard item name lookup against invalid ids
This commit is contained in:
+14
-2
@@ -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
|
||||
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user