Page MenuHomePhabricator

gsub error emitted by mw.text.trim()
Closed, DeclinedPublic



return mw.text.trim(nil)

will generate: Lua error: bad argument #1 to 'gsub' (string expected, got nil).
From a debugging perspective, this is pretty misleading, since gsub isn't used in the user-generated line of code, and there is no line number to aid the user. Are we able to either:

  1. make the error message refer to mw.text.trim()
  2. provide the offending line number as part of the error
  3. both?

If generalized, I'm assuming this could benefit the rest of the mw.text.* library and possibly other libraries. Original MW request here, and a related discussion here.

Event Timeline

Note that Scribunto provides the backtrace when you click the error that is output in the page, assuming you have JavaScript enabled:

Screenshot for T225227.png (312×648 px, 30 KB)

Inside Lua, if you're catching errors you can use xpcall() and use debug.traceback() inside the handler (or as the handler) to get the trace.

Generalizing the message itself is not all that likely: you'd need mw.text.trim() to pre-validate its parameters before calling gsub() and raise its own errors, and the same (with different validation code) for each other function. The more you try to validate against all possible errors, the more complex and slow that validation becomes.

Legoktm added a subscriber: Legoktm.

As Anomie notes, you can easily find the correct caller via the traceback.