Module:CallAssert/doc: Difference between revisions
Appearance
Content deleted Content added
<dependency of Module:Page> |
Dinoguy1000 (talk | contribs) this module is used on almost 200k pages, including system messages; this is inconsistent with an alpha rating (I have no opinion if the rating or the level of usage is "more" "correct", though changing the rating is considerably simpler and (ideally) less controversial than changing the level of usage) |
||
Line 1: | Line 1: | ||
{{Module rating| |
{{Module rating|r}} |
||
{{used in system}} |
{{used in system}} |
||
This [[Help:Lua metamodules|metamodule]] provides function '''callAssert''', which simplifies error checking by throwing an error if a given function returns nil or false. This is similar to the built-in function [[mw:LUAREF#assert|assert]], but unlike it, callAssert itself calls the function, whose return value is to be checked. This allows it to include the function name and argument values in the error message. |
This [[Help:Lua metamodules|metamodule]] provides function '''callAssert''', which simplifies error checking by throwing an error if a given function returns nil or false. This is similar to the built-in function [[mw:LUAREF#assert|assert]], but unlike it, callAssert itself calls the function, whose return value is to be checked. This allows it to include the function name and argument values in the error message. |
Revision as of 23:42, 15 November 2020
![]() | This Lua module is used in system messages, and on approximately 115,000 pages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
This metamodule provides function callAssert, which simplifies error checking by throwing an error if a given function returns nil or false. This is similar to the built-in function assert, but unlike it, callAssert itself calls the function, whose return value is to be checked. This allows it to include the function name and argument values in the error message.
Usage
local callAssert = require('Module:CallAssert') local result1, result2, etc = callAssert(func, 'funcName', ...)
callAssert
calls func(...)
and checks if the first returned value evaluates to true. If it does, then it returns all the returned values. If not, it throws an error with a message in the form 'funcName(argument_values) failed'
.