hi! guys as always i start a new week with a new problem and i ask help at the comunity!
in my new libs
i need to raise an error if the user pass a non valid argument to my class init...
example:
main.lua code (user code)
--
-- TNT Virtual Pad for Gideros Mobile SDK
-- Example 1
--
application:setKeepAwake(true)
application:setBackgroundColor(0x000000)
local back = Bitmap.new(Texture.new("sfondo3.png"))
--back:setPosition(0,0)
stage:addChild(back)
local vPad = CTNTVirtualPad.new(stage, "tntskinpad", PADMODE.USERERROR, BUTTONS.ONE, 10)
---------------------------------------------------------^^^^^^ ERROR! not valid argument!
vPad:start()
--vPad = vPad:free()
--vPad = nil |
in
13 local vPad = CTNTVirtualPad.new(stage, "tntskinpad", PADMODE.ANYVALUE, BUTTONS.ONE, 10) |
PADMODE array of strings (it's a string)
can be:
PADMODE.NO_STICK
PADMODE.SINGLE_STICK
PADMODE.DOUBLE_STICK
in this case user entered "PADMODE.ANYVALUE" and my lib raise this error:
tntvirtualpad.lua:209: bad argument #3 (padStyle) to 'CTNTVirtualPad New()' (string expected, got nil)
Valid arguments are:
'PADMODE.NO_STICK'
'PADMODE.SINGLE_STICK'
'PADMODE.DOUBLE_STICK'
the code that generare the error is :
function CTNTVirtualPad:init(parentStage, TNTVPadSkinPack, padStyle, padButtons, buttonSpace)
self.buttons = {}
self.properties = {
-- parent stage
parent = parentStage,
-- how buttons are defined (-1 none)
buttonsCount = -1,
-- load gfx Virtual pad pack
skin = TexturePack.new(TNTVPadSkinPack .. ".txt", TNTVPadSkinPack .. ".png"),
-- assign sprite analog pad
}
if type(padStyle) ~= "string" then
error("bad argument #3 (padStyle) to 'CTNTVirtualPad New()' (string expected, got " .. type(padStyle) .. ") \nValid arguments are: \n'PADMODE.NO_STICK'\n'PADMODE.SINGLE_STICK'\n'PADMODE.DOUBLE_STICK'")
end
if type(padButtons) ~= "string" then
error("bad argument #4 (padButtons) to 'CTNTVirtualPad New()' (string expected, got " .. type(padButtons) .. ") \nValid arguments are: \n'BUTTONS.NONE'\n'BUTTONS.ONE'\n'BUTTONS.TWO'\n'BUTTONS.THREE'\n'BUTTONS.FOUR'")
end
...
othere code here... |
now my question is:
ok my code "catch" the error but the compiler raise the line number and the filename of of my lib... (yes i know the error is detected here...) but the code is fine... and the user can be lost and confused by this error...
the error is really in the main.lua at line 13 ...
is there a way to release the function caller line number and it's file name (in this case main.lua) ? not the function line where the error occurs ?
a nice error handling and report would be:
--------------
main.lua:13: bad argument #3 (padStyle) to 'CTNTVirtualPad New()' (string expected, got nil)
Valid arguments are:
'PADMODE.NO_STICK'
'PADMODE.SINGLE_STICK'
'PADMODE.DOUBLE_STICK'
---------------
thanks.
(sorry but in'm not an expert lua coder)
ciao ciao
Gianluca.
Comments
And you should use level 2 in your case:
Likes: ndoss
I ignored the existence of optional parameter of "error" message.
with level 2
the error showed is:
-------------------
[string "property.lua"]:164: bad argument #3 (padStyle) to 'CTNTVirtualPad New()' (string expected, got nil)
Valid arguments are:
'PADMODE.NO_STICK'
'PADMODE.SINGLE_STICK'
'PADMODE.DOUBLE_STICK'
-------------------
what is "property.lua" ?
but with level 3
is correct:
-------------
main.lua:13: bad argument #3 (padStyle) to 'CTNTVirtualPad New()' (string expected, got nil)
Valid arguments are:
'PADMODE.NO_STICK'
'PADMODE.SINGLE_STICK'
'PADMODE.DOUBLE_STICK'
-------------
thanks!
www.tntengine.com
thanks!!!
ciao ciao!!
www.tntengine.com