mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-27 09:40:12 +00:00
98f25aa9a1
Add more information to error messages in mw.html. This includes the error level, the function name, and the position of the argument in the argument list. Where possible, use the functions in libraryUtil.lua to do this. Some functions in mw.html accept multiple types, so add a checkTypeMulti function to libraryUtil.lua to make these kinds of functions easy to check. And while we're at it, add test cases for libraryUtil.lua as well. Change-Id: If9cf9a52bd4b1bb42cc7f9f1f1096828710cbc52
72 lines
1.8 KiB
Lua
72 lines
1.8 KiB
Lua
local libraryUtil = {}
|
|
|
|
function libraryUtil.checkType( name, argIdx, arg, expectType, nilOk )
|
|
if arg == nil and nilOk then
|
|
return
|
|
end
|
|
if type( arg ) ~= expectType then
|
|
local msg = string.format( "bad argument #%d to '%s' (%s expected, got %s)",
|
|
argIdx, name, expectType, type( arg )
|
|
)
|
|
error( msg, 3 )
|
|
end
|
|
end
|
|
|
|
function libraryUtil.checkTypeMulti( name, argIdx, arg, expectTypes )
|
|
local argType = type( arg )
|
|
for _, expectType in ipairs( expectTypes ) do
|
|
if argType == expectType then
|
|
return
|
|
end
|
|
end
|
|
local n = #expectTypes
|
|
local typeList
|
|
if n > 1 then
|
|
typeList = table.concat( expectTypes, ', ', 1, n - 1 ) .. ' or ' .. expectTypes[n]
|
|
else
|
|
typeList = expectTypes[1]
|
|
end
|
|
local msg = string.format( "bad argument #%d to '%s' (%s expected, got %s)",
|
|
argIdx,
|
|
name,
|
|
typeList,
|
|
type( arg )
|
|
)
|
|
error( msg, 3 )
|
|
end
|
|
|
|
function libraryUtil.checkTypeForIndex( index, value, expectType )
|
|
if type( value ) ~= expectType then
|
|
local msg = string.format( "value for index '%s' must be %s, %s given",
|
|
index, expectType, type( value )
|
|
)
|
|
error( msg, 3 )
|
|
end
|
|
end
|
|
|
|
function libraryUtil.checkTypeForNamedArg( name, argName, arg, expectType, nilOk )
|
|
if arg == nil and nilOk then
|
|
return
|
|
end
|
|
if type( arg ) ~= expectType then
|
|
local msg = string.format( "bad named argument %s to '%s' (%s expected, got %s)",
|
|
argName, name, expectType, type( arg )
|
|
)
|
|
error( msg, 3 )
|
|
end
|
|
end
|
|
|
|
function libraryUtil.makeCheckSelfFunction( libraryName, varName, selfObj, selfObjDesc )
|
|
return function ( self, method )
|
|
if self ~= selfObj then
|
|
error( string.format(
|
|
"%s: invalid %s. Did you call %s with a dot instead of a colon, i.e. " ..
|
|
"%s.%s() instead of %s:%s()?",
|
|
libraryName, selfObjDesc, method, varName, method, varName, method
|
|
), 3 )
|
|
end
|
|
end
|
|
end
|
|
|
|
return libraryUtil
|