Jump to content
Wikipedia The Free Encyclopedia

Module:Protect

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
[画像:Warning] This Lua module is used in system messages, and on approximately 121,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 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

localprotect=require('Module:Protect')
localprotectedFunc=protect(func,errFormat,options)

Arguments

  • func
    Function to be transformed.
  • errFormat (default: 'Error: %s')
    Custom error message.
    Use '%s' to include the message from a caught error.
  • options – optional table with the following fields:
    • raw (default: false)
      If true, then errFormat will be used as is, otherwise it will be wrapped inside a tag <strong class="error">.
    • removeLocation (default: true)
      If true, removes location information from caught error messages.

Return value

The resulting protectedFunc is a function, which calls the original function func, passing all arguments to it, and returns all its return values. If func throws an error, the specified error message is returned instead.

Example

localprotect=require('Module:Protect')
localp={}
functionp.main(frame)
ifnotframe.args[1]then
error('missing argument')
end
returnframe.args[1]
end
p.main=protect(p.main)
returnp

Invoking the main function without arguments will output: Error: missing argument

The above documentation is transcluded from Module:Protect/doc. (edit | history)
Editors can experiment in this module's sandbox (create | mirror) and testcases (edit | run) pages.
Subpages of this module.

 localfunctionprocessResult(options,success,...)
 ifnotsuccessthen
 localmessage=tostring(...or'(no message)')
 ifoptions.removeLocationthen
 message=string.gsub(message,'^Module:[^:]+:%d+: ','',1)
 end
 returnstring.format(options.errFormat,message)
 end
 return...
 end

 localfunctionprotect(func,errFormat,options)
 iftype(errFormat)=='table'then
 options=optionsorerrFormat
 errFormat=nil
 end
 options=mw.clone(options)or{}
 options.errFormat=errFormatoroptions.errFormator'Error: %s'
 ifnotoptions.rawthen
 options.errFormat='<strong class="error">'..options.errFormat..'</strong>'
 end
 options.removeLocation=options.removeLocation==niloroptions.removeLocation

 returnfunction(...)
 returnprocessResult(options,pcall(func,...))
 end
 end

 returnprotect

AltStyle によって変換されたページ (->オリジナル) /