Module: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