Page MenuHomePhabricator

gsub error emitted by mw.text.trim()
Open, Needs TriagePublic

Description

Currently,

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 en.wiki discussion here.

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald TranscriptJun 6 2019, 3:47 PM
Anomie added a subscriber: Anomie.Jun 10 2019, 7:16 PM

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

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.