模块:Protect/doc
这是Module:Protect的文档页面
这个模板被使用在系统讯息中。 对此模板的修改会立即反映在维基百科用户介面上,为避免造成大规模的影响,任何编辑应该先在此模板的沙盒页面、测试样例或您自己的用户子页面中测试,测试过后的修改才能在单次编辑中添加到此模板。请在做任何修改之前在此讨论页中进行讨论。 |
This metamodule simplifies error handling in other modules. It transforms a function, which may throw an error, into a function, which returns a specified error message in that case.
Usage
编辑 local protect = require('Module:Protect')
local protectedFunc = protect(func, errFormat, options)
Arguments
编辑func
- Function to be transformed.
(default:errFormat
)'Error: %s'
- Custom error message.
- Use
to include the message from a caught error.'%s'
– optional table with the following fields:options
(default: false)raw
- If true, then
will be used as is, otherwise it will be wrapped inside a tagerrFormat
<strong class="error">
.
- If true, then
(default: true)removeLocation
- If true, removes location information from caught error messages.
Return value
编辑The resulting
is a function, which calls the original function protectedFunc
, passing all arguments to it, and returns all its return values. If func
throws an error, the specified error message is returned instead.
func
Example
编辑local protect = require('Module:Protect')
local p = {}
function p.main(frame)
if not frame.args[1] then
error('missing argument')
end
return frame.args[1]
end
p.main = protect(p.main)
return p
Invoking the main function without arguments will output: Error: missing argument