rephrase "wrong argument type" errors#4599
Conversation
|
Hey there! Thanks for helping Mudlet improve. 🌟 Test versionsYou can directly test the changes here:
No need to install anything - just unzip and run. |
|
Yep that was quite a text reducing spree 😁 Only a few doubts remain:
Noted but not in scope:
|
I suspect it doesn't do it too well, need to investigate
Need to update manual docs in this case |
SlySven
left a comment
There was a problem hiding this comment.
There are other places where this code can be refactored in, e.g.:
addRoom(...)setDoor(...)
👍 Ah, you are only targetting the "wrong argument" cases at the moment - leaving scope for further refactoring...!
| } | ||
| int luaSleepMsec = lua_tointeger(L, 1); | ||
|
|
||
| int luaSleepMsec = getVerifiedInt(L, __func__, 1, "sleep time in msec"); |
| if (!dirType) { | ||
| lua_pushstring(L, "setExitStub: Need a dir number as 2nd argument"); | ||
| int dir = dirToNumber(L, 2); | ||
| if (!dir) { |
There was a problem hiding this comment.
🤔 For an invalid string (not matching those defined in dirToNumber(...)) or a invalid number (not in range 1 to 12) that function will also return a 0 but that is not a type error but a run-time value error so should not be aborting the calling Lua script - only an invalid argument type should be doing that - therefore there should be a
if (!(lua_isnumber(L, 2) || lua_isnumber(L, 2))) {
test before the dirToNumber(...)call in this area IMHO - and that should trigger the error here.
There was a problem hiding this comment.
😊 Ooops - meant to say:
if (!(lua_isnumber(L, 2) || lua_isstring(L, 2))) {
| } | ||
| pR->setExitStub(dirType, status); | ||
| pR->setExitStub(dir, status); | ||
| return 0; |
There was a problem hiding this comment.
Given all the other error handling changes I guess it would not be unreasonable to actually return a true if we get here to indicate success!
| if (dirType > 12 || dirType < 1) { | ||
| lua_pushstring(L, "setExitStub: dirType must be between 1 and 12"); | ||
| if (dir > 12 || dir < 1) { | ||
| lua_pushstring(L, "setExitStub: direction must be between 1 and 12"); |
There was a problem hiding this comment.
The range of dir has already been clamped to 0 to 12 by dirToNumber(...) and that first value is the error case that represents either an invalid direction number OR an unrecognised as an (Engineering-English) exit string. So this text needs revising. Also as those are run-time value errors they should return a nil+error message and not abort the calling Lua script...
... the message should, of course not contain setExitStub: but actually indicate the number or string that was supplied by the caller.
There was a problem hiding this comment.
If you are not going to address this other problem then we'll need a new issue along the lines of:
"Some lua functions that take a (normal) exit as a direction number or a string return confusing error messages for invalid exit strings"
i.e. give the exit direction as "downwards" and the function will abort with a message
"xxxx: direction must be between 1 and 12" - even though a string rather than a number was given.
src/TLuaInterpreter.cpp
Outdated
|
|
||
| if (!lua_isnumber(L, 2)) { | ||
| lua_pushstring(L, "resizeWindow: wrong argument type"); | ||
| lua_pushfstring(L, "resizeWindow: bad argument #2 type (x as number expected, got %s!)", luaL_typename(L, 2)); |
| float height = getVerifiedFloat(L, __func__, 7, "height"); | ||
| float zoom = getVerifiedFloat(L, __func__, 8, "zoom"); | ||
| bool showOnTop = getVerifiedBoolean(L, __func__, 9, "zoom"); | ||
|
|
There was a problem hiding this comment.
💭 There is now an option to NOT scale the image - haven't tried playing with it to see how it acts in conjunction with the scaling - but it might be desirable to add the later added feature to the Lua API.
| bool gridMode = lua_toboolean(L, 2); | ||
|
|
||
| int area = getVerifiedInt(L, __func__, 1, "areaID"); | ||
| bool gridMode = getVerifiedBoolean(L, __func__, 2, "true/false"); |
There was a problem hiding this comment.
grid mode rather than true/false - like the other cases it is implicit that true means grid mode "on".
| } | ||
| QString sendText = lua_tostring(L, 1); | ||
|
|
||
| QString sendText = getVerifiedString(L, __func__, 1, "sendText"); |
|
|
||
| if (!lua_isstring(L, 2)) { | ||
| lua_pushstring(L, "setLabelStyleSheet: wrong argument type"); | ||
| lua_pushfstring(L, "setLabelStyleSheet: bad argument #2 type (markup as string expected, got %s!)", luaL_typename(L, 2)); |
There was a problem hiding this comment.
Maybe style sheet rather than markup ?
| return lua_error(L); | ||
| } | ||
| QString tidiedWhat = QString(lua_tostring(L, 1)).toLower().trimmed(); | ||
| QString tidiedWhat = getVerifiedString(L, __func__, 1, "style", true).toLower().trimmed(); |
There was a problem hiding this comment.
format rather than style perhaps?
|
Isn't it funny how much more obvious naming and logic become once the clutter is out the way? 😀 I took most name suggestions from wiki documentation (like the one letter names, and in fact the wide variety of names for color components in different functions) and do not plan aligning those during this PR right here. Unless of course you found some more mix ups and copy paste errors, which I gladly take up. Also most other logic remained unchanged, so all 💭 should move to their own issue for future reference. The functions calling dirToNumber() I actually found quite difficult to touch because they mix type and value error handling, in an external function, with an argument that can be multi type as well. I hope I did not destroy any existing logic here, merely trying to add an understandable message to the (messy) status quo. |
|
@SlySven FYI I commented your bunch of suggestions with thumb up and down which indicate NOT if I support the suggestion in general, as there were actually quite a few I liked, like better names for variables etc. BUT if they led to changes included in this PR scope after all or no changes done. |
|
The thing is that you are introducing names for arguments to the lua functions - in describing that they are wrong - so it makes sense so that the terms that are used makes sense. So for the elements of a RGB colour specification, for instance " |
And both read better than the current
Wrong, the wiki (remember what's the central source of documentation #1150 😏) introduced these names long ago: |
Yes please do open a new issue to untangle all dirToNumber usages. It's easy, just a click on the green button at top, and collect ideas and code snippets there. In fact, would be better than spreading all the ideas over multiple comment threads in this pr as well as duplicated again in the new issue you're hopefully already starting to create.
The functions came in #4593 and I'd be glad to see them used more. However this PR here merely uses them to remove all "wrong argument type" messages. While doing that I noted a bunch additional things and listed them above as out-of-scope. Thumbs relate to scope as explained above. Feel free to tackle those currently ignored errors or any of the other points really. |
Done. |
No, that seems like a copy/paste error, compare insertPopup() where that string is used as well, but in setPopup() it is never used at all. How should we update the docs then? |






Brief overview of PR changes/additions
Bring all TLuaInterpreter functions forward to current standards.
Motivation for adding to Mudlet
Never just show "wrong argument type" again without informing which argument, which type expected, etc.
Other info (issues closed, discussion etc)
Another large step for #2091 mostly wielding #4593 where possible